Copied to clipboard

Flag this post as spam?

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


  • Karen 8 posts 98 karma points
    Jul 12, 2019 @ 10:08
    Karen
    0

    Site navigation

    Hello, I am trying to create a two level navigation for my site. I have changed the order of the code and get various error messages the latest being error -

    CS1928: 'System.Collections.Generic.IEnumerable ... does not contain a definition for 'Any'

    I also get the name secondLevel does not exist in the current context even though the var is above the code block where it will be used.

    I'm very confused if someone could explain why the code below is not working I would be very grateful.

    The code from the Template linking to this partial is @Html.Partial("V6NavigationTopLevel-2")

    Thank you

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @* Displays child pages below the root page of a standard website. *@

    @{

    @* Get the root of the website *@
    var root = Umbraco.AssignedContentItem.AncestorOrSelf(1);
    var firstLevel = root.Children.Where("Visible");
    
    
    @* dispaly first level *@
    foreach(var sectionPage in firstLevel)
    {       
        <ul role="navigation" aria-label="Primary" class="nav navbar-nav">
            <li>
                <a href="@sectionPage.Url">@sectionPage.Name</a>
                @* display secon level *@
                @if (sectionPage.Children.Any("Visible"))
                {
                    var secondLevel = sectionPage.Children.Where("Visible");
    
                    <ul aria-label="Secondary" class="">
                        @foreach (var subSection in secondLevel)
                        {
                            @* display subSection here *@
                            <li>
                                <a href="@subSection.Url">@subSection.Name</a>  
                            </li>
                        }
                    </ul>
                }
    
            </li>
        </ul>
    
    }   
    

    }

  • Karen 8 posts 98 karma points
    Jul 12, 2019 @ 11:10
    Karen
    1

    Hi, I have the solution - you can't place the Visible in the any() it needs to go in the where() and then you activate the boolean any. So the correct code is: -

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @* Displays child pages below the root page of a standard website. *@

    @{

    @* Get the root of the website *@
    var root = Umbraco.AssignedContentItem.AncestorOrSelf(1);
    var firstLevel = root.Children.Where("Visible");
    
    
    @* Second level *@
    foreach(var sectionPage in firstLevel)
    {       
        <ul role="navigation" aria-label="Primary" class="nav navbar-nav">
            <li>
                <a href="@sectionPage.Url">@sectionPage.Name</a>
                @if (sectionPage.Children.Where("Visible").Any())
                {
                    var secondLevel = sectionPage.Children.Where("Visible");
    
                    <ul aria-label="Secondary" class="">
                        @foreach (var subSection in secondLevel)
                        {
                            // display subSection here
                            <li>
                                <a href="@subSection.Url">@subSection.Name</a>  
                            </li>
                        }
                    </ul>
                }
    
            </li>
        </ul>
    
    }   
    

    }

    Thank you, I hope this is useful for others :)

Please Sign in or register to post replies

Write your reply to:

Draft