Copied to clipboard

Flag this post as spam?

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


  • John Halsey 59 posts 220 karma points
    Mar 05, 2014 @ 12:33
    John Halsey
    0

    Problem with simple foreach loop in Razor

    I'm trying to write a simple foreach loop with c# razor and am getting stuck. I feel sure I've done something obviously wrong.

    I am attemping to loop through all nodes with a certain doc type of "Product" (located in different folders in my content tree) and list out their node names.

    This is what I have so far:

    var products = Model.NodeTypeAlias("Product").Visible;
    <ul>
    @foreach(var product in products)
    {
       <li>product.Name</li>
    }
    </ul>
    

    But I'm getting "Error loading Macro" on screen.

    Seems like it should be simple but I can't work it out.

    Thank you.

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 05, 2014 @ 12:58
    Fuji Kusaka
    100

    Hi John

    Try this instead 

    var products = Model.AncestorOrSelf(1).Descendants("Product").Where("Visible").Items;
  • John Halsey 59 posts 220 karma points
    Mar 05, 2014 @ 13:39
    John Halsey
    0

    Perfect, thank you. I really need to learn Razor properly :(

Please Sign in or register to post replies

Write your reply to:

Draft