Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steve 472 posts 1216 karma points
    Sep 25, 2013 @ 20:19
    Steve
    0

    Creating a list of parents and their children using @helper traverse

    I am new to razor and am trying to create a navigational list consisting of parent nodes and their children, but I am having no luck getting it to work with nested foreach loops, so I am trying to use @helper traverse(dynamic node).

    Here is what I have, but it doesn't work:

    @{ 
        var page = Model.DescendantsOrSelf("AcademicsCategory").Where("Visible");
        var subs = page.Children.Where("Visible");      
    }   
    
    @helper traverse(dynamic node){         
    
        <ul style="border:1px solid red">
        @foreach(var item in page) {
            <li>@item.Name
                <ul>
                @traverse(subs)
                <li>@child.Name</li>
                        </ul>
                    </li> 
                  }
     </ul>
    }
    
    
    
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 25, 2013 @ 20:34
    Ali Sheikh Taheri
    0

    Hi Steve

    This should work if you have 2 levels.

    @foreach (var subPage in page)
    {
        <ul>
            <li>
                @subPage.Name
                <ul>
                    @foreach (var item in subPage.Children)
                    {
                        <li>@item.Name</li>
                    }
                </ul>
            </li>
        </ul>
    }
    

    Cheers Ali

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 25, 2013 @ 21:09
    Jeavon Leopold
    0

    I think you have problem in that page is a collection not a single node. Maybe try the below and then Ali's suggestion

    var page = Model.DescendantsOrSelf("AcademicsCategory").Where("Visible").First().Children;
    
  • Steve 472 posts 1216 karma points
    Sep 25, 2013 @ 21:10
    Steve
    0

    Thanks Ali! I wish I understood how to do it with a (@helper) as well.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 25, 2013 @ 21:15
    Jeavon Leopold
    0

    Hi Steve,

    You can do it with a traverse helper, it's a bit more complicated and written for Mvc but take a look at this

    Jeavon

  • Steve 472 posts 1216 karma points
    Sep 25, 2013 @ 21:17
    Steve
    0

    Thanks, you are right. A little too much for me. Thanks anyway.

Please Sign in or register to post replies

Write your reply to:

Draft