Copied to clipboard

Flag this post as spam?

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


  • Elad Lachmi 112 posts 144 karma points
    Apr 19, 2012 @ 11:27
    Elad Lachmi
    0

    Multi-level menu not working

    Hi,

    I am new to Razor and I am having some trouble.

    I have the following code inline:

    <umbraco:Macro Language="cshtml" runat="server" recursive="true">
                          @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Visible"))
                          {
                             <li><class="menu-1" href="@page.Url">@page.Name</a>
                          @if (page.Children.Where("Visible").Count() > 0)
                             {
                                <li><class="sub1" href="@page.Url">@page.Name</a>
                                <ul>
                                 @foreach (var subpage in page.Children.Where("Visible"))
                                 {
                                    <li><href="@subpage.Url">@subpage.Name</a></li>
                                 }
                                </ul>
                               </li>
                             }
                             else
                            {
                              <li><class="menu-1" href="@page.Url">@page.Name</a></li>
                            }
                            
                          }
                        </umbraco:Macro>

    This gives me: Error loading MacroEngine script (file: )

    I can't figure out what the issue is.

    Thank you!

  • Elad Lachmi 112 posts 144 karma points
    Apr 19, 2012 @ 11:30
    Elad Lachmi
    0

    I can't seem to edit the post, so I can't delete the first <li> after the @foreach.

    It is a copying error. Sorry.

  • Douglas Ludlow 210 posts 366 karma points
    Apr 20, 2012 @ 17:15
    Douglas Ludlow
    1

    This seems to work:

    @{
    <ul>
    @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Visible"))
    {
    if (page.Children.Where("Visible").Count() > 0)
    {
    <li><a class="sub1" href="@page.Url">@page.Name</a>
    <ul>
    @foreach (var subpage in page.Children.Where("Visible"))
    {
    <li><a href="@subpage.Url">@subpage.Name</a></li>
    }
    </ul>
    </li>
    }
    else
    {
    <li><a class="menu-1" href="@page.Url">@page.Name</a></li>
    }
    }
    </ul>
    }

    You had an extra @ sign in front of your first if statement.

  • Elad Lachmi 112 posts 144 karma points
    Apr 20, 2012 @ 17:36
    Elad Lachmi
    0

    Yep. That was it. Silly me :) I'm so new to Razor I'm not even 100% on the syntax. I should get a book or something.

    Thank you!

  • Douglas Ludlow 210 posts 366 karma points
    Apr 20, 2012 @ 17:58
    Douglas Ludlow
    0

    Yeah, and then Umbraco 4's razor has some things only specific to it as well.

Please Sign in or register to post replies

Write your reply to:

Draft