Copied to clipboard

Flag this post as spam?

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


  • seanrock 239 posts 460 karma points
    Oct 06, 2011 @ 21:36
    seanrock
    0

    nested foreach

    I'm trying to create a nested foreach to iterate over the parent and then the children.

    @foreach(var cat in @Model.Children)
      {   
         <div>@cat.Name</div>
         foreach(var prod in cat.Children)
         {
            @prod.Name
         }
      }

     

    Obviously this isn't working because @cat.Children is null. Any ideas how i can do this?

    Thanks.

     

     

  • Mr A 216 posts 278 karma points
    Oct 07, 2011 @ 18:12
    Mr A
    0

    Do you want to show all the nodes including children and their parents or you are looking for specific nodes??


  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 07, 2011 @ 21:02
    Dan Diplo
    0

    Your code is fine. If it's not working then the page you are on doesn't have any children or they're not published. But fundamentally what you are doing is exactly right.

  • seanrock 239 posts 460 karma points
    Oct 09, 2011 @ 11:56
    seanrock
    0

    @Mr A - all nodes, i.e. i'm creating a list of categories and a list of its products so image a nested repeater, outer loop iterates over the categories and for each category iterate over the products. thanks.

     

    @Dan - ok i'm not perfect :) so i'll check again. thanks.

  • Markus Johansson 1910 posts 5734 karma points MVP c-trib
    Oct 10, 2011 @ 08:28
    Markus Johansson
    0

    if you get an null-error maybe some of the cats don't have a child? You could try a null-check?

    if(cat.Children != null)

     

  • seanrock 239 posts 460 karma points
    Oct 10, 2011 @ 09:07
    seanrock
    0

    Strange, all nodes are defo published however i republished entire tree then it started working.

Please Sign in or register to post replies

Write your reply to:

Draft