Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Sep 19, 2013 @ 10:39
    Anthony Candaele
    0

    Top Menu Navigation with v6 Razor

    Hi, anyone yet done a top menu navigation Partial View Macro File with v6 Razor?

     

    thanks for your help,

    Anthony

  • Ali Sheikh Taheri 459 posts 1604 karma points c-trib
    Sep 19, 2013 @ 10:49
    Ali Sheikh Taheri
    100
    @{
        var root = Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible());
    }
    
    <nav>
        @foreach (var page in root)
        {
             var pageUrl = page.DocumentTypeAlias == "HomePage" ? "/" : page.Url;
             var cssClass = page.Id == CurrentPage.AncestorOrSelf(2).Id ? "active" : string.Empty;
    
                <a class="@cssClass" href="@pageUrl">@page.GetPropertyValue("title",page.Name)</a>
        }  
    </nav>
    
  • Anthony Candaele 1197 posts 2049 karma points
    Sep 19, 2013 @ 10:52
    Anthony Candaele
    0

    @Ali, again you come to the rescue.

    Thanks a lot!

    greetings,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 19, 2013 @ 11:18
    Anthony Candaele
    0

    Hi Ali, your script works like a charm, I have one issue to solve however. All my pages are subnodes of Home

     

     

    so currently the home node is not showing in the top menu navigation

     

    so is there a way to adapt the script so the home node gets rendered as well?

     

    Thanks for your help,

    Anthony

  • Jeavon Leopold 2999 posts 13137 karma points MVP 6x admin c-trib
    Sep 19, 2013 @ 12:46
    Jeavon Leopold
    1

    Hi Anthony,

    You might benefit from taking a look at NavIt It just installs a single flexible Partial View.

    Jeavon

  • Ali Sheikh Taheri 459 posts 1604 karma points c-trib
    Sep 19, 2013 @ 12:46
    Ali Sheikh Taheri
    1

    This should work

    @{
        var root = Model.Content.AncestorOrSelf(1);
        var rootChildern = root.Children.Where(x => x.IsVisible());
    }
    
    <nav>
        <a class="@(root.Id == Model.Content.Id ? "active" : string.Empty)" href="@root.Url">@root.Name</a>
        @foreach (var page in rootChildern)
        {
            var cssClass = page.Id == CurrentPage.AncestorOrSelf(2).Id ? "active" : string.Empty;
    
            <a class="@cssClass" href="@page.Url">@page.GetPropertyValue("title", page.Name)</a>
        }
    </nav>
    
  • Anthony Candaele 1197 posts 2049 karma points
    Sep 19, 2013 @ 13:25
    Anthony Candaele
    0

    HI Ali,

    It works, except when CurrentPage is the Homepage itself, then this line:

    var cssClass = page.Id == CurrentPage.AncestorOrSelf(2).Id ? "active" : string.Empty;

    throws a null reference exception:

    "Cannot perform runtime binding on a null reference"

    I already tried to solve it by changing the level:

    var cssClass = page.Id == CurrentPage.AncestorOrSelf(1).Id ? "active" : string.Empty;

    This solves the null reference error when CurrentPage is the Homepage but then the class="active" doesn't get added to the anchor link of the other nodes

  • Ali Sheikh Taheri 459 posts 1604 karma points c-trib
    Sep 19, 2013 @ 14:30
    Ali Sheikh Taheri
    1

    this should fix it

    var cssClass = page.Id == Model.Content.Id ? "active" : string.Empty;
    
  • Anthony Candaele 1197 posts 2049 karma points
    Sep 19, 2013 @ 14:57
    Anthony Candaele
    0

    it does, thanks a lot!

  • Ali Sheikh Taheri 459 posts 1604 karma points c-trib
    Sep 19, 2013 @ 15:09
    Ali Sheikh Taheri
    1

    you are welcome Anthony!

  • Pratham 4 posts 24 karma points
    Apr 24, 2015 @ 08:30
    Pratham
    0

    Hi, I have mention below so my code related Menu. It's work for me but i want to display same navigation with other page means, which is not related with that all dynamic page. I have created some manually page, and in that I want same menu which is displayed on my all other page. so, can you guide me or suggest me how can i do ?

        @inherits UmbracoTemplatePage
    @{   
        var homePage = CurrentPage.AncestorsOrSelf(1).First();   
        var menuItems = homePage.Children.Where("UmbracoNaviHide  == false");    
    }
    
    <div id="navbar-collapse-1" class="navbar-collapse collapse pull-right">
    <div class="menu-main-menu-container">
        <nav class="mobile-nav-primary mobile-menu">
            <ul id="devn-mainmenu" class="nav navbar-nav">
                @{
                    var siteroot = CurrentPage.AncestorOrSelf(1);
                }
                @foreach (var page in siteroot.Children.Where("!UmbracoNaviHide"))
                {
    
                    if (page.Children.Count() > 0)
                    {
                        <li class="dropdown yamm-fw">
                            <a href="@page.Url()">@page.Name</a>
                            <ul class="dropdown-menu" role="menu">
    
                                @foreach (var subpage in page.Children.Where("!UmbracoNaviHide"))
                                {
    
                                        if (page.Children.Count() < 1)
                                        {
                                <li><a href="@subpage.Url">@subpage.Name</a></li>
                                        }
                                        else
                                        {
                                <li>
                                    <ul class="col-sm-6 col-md-4 list-unstyled two">
                                        <li>
                                            <p><a href="@subpage.Url">@subpage.Name</a></p>
                                            <p style="border-bottom: 1px solid #e6e6e6;margin-bottom: 10px;"></p>
                                        </li>
                                        @foreach (var subsubpage in subpage.Children.Where("!UmbracoNaviHide"))
                                                    {
                                                        if (subsubpage.Children.Count() < 1)
                                                        {
                                                    <li>
                                                        <i class="fa fa-caret-right"></i><a href="@subsubpage.Url">@subsubpage.Name</a>
                                                    </li>
                                                        }
                                                        else
                                                        {
                                                    <li>
                                                        <div class="row" style="margin:0px !important;">
                                                            <ul class="col-sm-6 col-md-4 list-unstyled two" style="padding-left:0px !important;">
                                                                <li>
                                                                    <i class="fa fa-caret-right"></i><a href="@subsubpage.Url">@subsubpage.Name</a>
                                                                </li>
                                                                @foreach (var subsubsspage in subsubpage.Children.Where("!UmbracoNaviHide"))
                                                                        {
                                                                        <li>
                                                                            &nbsp;&nbsp;&nbsp; <i class="fa fa-caret-right"></i><a href="@subsubsspage.Url">@subsubsspage.Name</a>
                                                                        </li>
                                                                        }
                                                            </ul>
                                                        </div>
                                                    </li>
                                                        }
                                                    }
                                    </ul>
                                </li>
                                        }
                                }
                            </ul>
                        </li>
                    }
                    else
                    {
                        <li><a href="@page.Url">@page.Name</a></li>
                    }
                }
    
            </ul>
        </nav>
    </div>
    

    Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft