Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


MVC Razor how to loop through nested data?

Posted on 2014-02-07
Medium Priority
Last Modified: 2014-02-07

I have a data structure with nested location objects and in every level of the loction object is it possible to have resources assigned.



  -- Location
    -- Resource
    -- Resource
-- Location
  -- Resource

Open in new window

Now I would like to render it using RAZOR in a nested way. Normally I would use recursive methods for it. But I don't think that's possible with RAZOR

what is the best practice for something like this?
Question by:HugoHiasl
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 39842170
Why not create a Razor helper that can process this for you?


@model IEnumerable<Location>

@using MvcApplication22.Models

@helper RenderLocations(IEnumerable<Location> locations)
    foreach (Location location in locations)

@helper RenderLocation(Location location)

        @foreach (var subLocation in location.Locations)

        @foreach (var resource in location.Resources)

@helper RenderResource(Resource resource)


Open in new window

Helpers are just bits of re-usable code that pack neatly into your view (or you can break them out into a separate class that your view can reference). They are useful for complicated rendering scenarios like what you are facing here.

More Info:
LVL 12

Author Closing Comment

ID: 39842227
The @helper and @functions keywords were the part that I did not know... I am fairly new to MVC... Thanks.
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39842242
Normally, once you are inside a Razor block you don't need to prefix your code with an @. However, in my testing (and research) I found that when you call one helper from another you need to prefix the call with the @. This is why lines 9, 21, and 28 have the leading @.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question