Copied to clipboard

Flag this post as spam?

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


  • Mikkel Johansen 10 posts 91 karma points
    Nov 14, 2017 @ 07:34
    Mikkel Johansen
    0

    Subnavigation showing parent

    Hi I have a problem creating a subnavigation i razor, thats shows parent along with the childpages. I have a structure like this:

    Frontpage

    • About
    • Services
    • --- Service 1
      ---- Service 2
      ---- Service 3
    • Cases
    • Contact

    When you click on "Services", I need the subnavigation to show not only the childpages (service 1, service 2 etc.) but also the parent "Services".

    I am using the standard Razor template for navigations, but I don't know how to customize it to also include the parent. Heres my Razor script:

    @{ 
        @*Get the root of the website *@
        var root = Model.AncestorOrSelf(2);
    }
    
    <nav class="second-nav">
        @foreach (var page in root.Textpages.Where("Visible"))
        { 
    
                <a class="@page.IsAncestorOrSelf(Model, "current", "")" href="@page.Url">@page.name</a>
    
        }
    </nav>
    

    Can someone please help me out?

  • Ben Palmer 115 posts 577 karma points
    Nov 14, 2017 @ 07:51
    Ben Palmer
    100

    You could simply 'hard-code' the first item using root, something like below.

    Also, just to note that you probably want to null check your variables here so I've added that in to the example aswell. Finally, I'm using Children here as I'm running Umbraco 7.7 and TextPages throws an error.

    @{ 
        var root = Model.Content.AncestorOrSelf(2);
    }
    
    <nav class="second-nav">
        @if (root != null)
        {
            <a class="@root.Url">@root.Name</a>
    
            if (root.Children != null && root.Children.Any())
            {
                foreach (var child in root.Children)
                {
                    <a class="@child.IsAncestorOrSelf(Model.Content, "current", "")" href="@child.Url">@child.Name</a>
                }
            }
        }
    </nav>  
    
  • Mikkel Johansen 10 posts 91 karma points
    Nov 14, 2017 @ 07:57
    Mikkel Johansen
    0

    Perfect - Thats solved my problem. Thank you, Ben :)

    -- Big high five --

Please Sign in or register to post replies

Write your reply to:

Draft