[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MVC Razor how to loop through nested data?

Posted on 2014-02-07
3
Medium Priority
?
3,179 Views
Last Modified: 2014-02-07
Hi,

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

[u]Location-Object[/u]
Id
ParentLocation
LocationName

[u]Resource-Object[/u]
Id
LocationId
ResourceName



--Location
  -- 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?
0
Comment
Question by:HugoHiasl
  • 2
3 Comments
 
LVL 75

Accepted Solution

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

e.g.

@model IEnumerable<Location>

@using MvcApplication22.Models

@helper RenderLocations(IEnumerable<Location> locations)
{
    foreach (Location location in locations)
    {
        @RenderLocation(location);
    }
}

@helper RenderLocation(Location location)
{
    <div>
        <div>@location.LocationName</div>

        <ul>
        @foreach (var subLocation in location.Locations)
        {
            <li>@RenderLocation(subLocation)</li>
        }
        </ul>

        <ul>
        @foreach (var resource in location.Resources)
        {
            <li>@RenderResource(resource)</li>
        }   
        </ul>         
    </div>
}

@helper RenderResource(Resource resource)
{
    <div>@resource.ResourceName</div>
}

<div>@RenderLocations(Model)</div>

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:
http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx
http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-helpers,-forms-and-validation
0
 
LVL 12

Author Closing Comment

by:HugoHiasl
ID: 39842227
The @helper and @functions keywords were the part that I did not know... I am fairly new to MVC... Thanks.
0
 
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 @.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

830 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