Copied to clipboard

Flag this post as spam?

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


  • Cedeste 37 posts 129 karma points
    Feb 01, 2023 @ 15:24
    Cedeste
    0

    Umbraco UmbNav

    Hi there - new umbraco developer.

    I'm trying to implement the Umbraco UmbNav found here: https://www.nuget.org/packages/Our.Umbraco.UmbNav.Web

    I've completed the install and have the new property type. I've added it to my homepage doc type and have the menu created.

    master page

    <!-- Navigation-->
          @await Html.PartialAsync("Navigation")
    

    navigation partial

    <!-- Navigation-->
         <nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="mainNav">
        <div class="container px-4 px-lg-5">
            <a class="navbar-brand" href="@homePage.Url()">Test Site</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                Menu
                <i class="fas fa-bars"></i>
            </button>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ms-auto py-4 py-lg-0">
    
                      @await Html.PartialAsync("Partials/Navigation/TopNavigation")
                </ul>
            </div>
        </div>
    </nav>
    

    Top and Sub Navigation Code is pulled directly from the github here.

    Site loads with no error, but the menu will not display. When I inspect, I see the following.

    <ul class="navbar-nav ms-auto py-4 py-lg-0">
    
    
    <!-- comment out the following line if you want to remove the root node name from the top navigation -->
    <a class="nav-link navi-link--active" href="/">Home</a>
        <umbnavitem menu-item="item" active-class="nav-link--active" class="nav-link">
    
        </umbnavitem>
        <umbnavitem menu-item="item" active-class="nav-link--active" class="nav-link">
    
        </umbnavitem>
    
                </ul>
    

    I am not sure what I am missing, but any advice would be greatly appreciated.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 01, 2023 @ 15:30
    Huw Reddick
    0

    did you add the code for the taghelper in your _ViewImports.cshtml ?

    This code

    @addTagHelper *, UmbNav.Core
    
  • Cedeste 37 posts 129 karma points
    Feb 01, 2023 @ 15:43
    Cedeste
    0

    Thank you.

    That fixed the display of the top nav, but now the dropdowns aren't working.

    When I inspect I see:

    <ul class="navbar-nav ms-auto py-4 py-lg-0">
    
    
    }
        <a class="nav-link nav-link--active" href="/">Home</a>
        <a class="nav-link" href="/about/">About</a>
        <span class="nav-link">Test</span>
    
    
                </ul>
    

    Test should have have two dropdowns below it.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 02, 2023 @ 09:59
    Huw Reddick
    0

    Hi Cedeste,

    You will need to use something like below in your _TopNavigation template

    @foreach (UmbNavItem item in umbNav)
    {
        <ul class="navbar-nav">
            @if (item.Children.Any())
            {
                <li class="nav-item dropdown" >
                    <umbnavitem menu-item="item" active-class="nav-link--active" class="nav-link"></umbnavitem>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        @foreach (var child in item.Children)
                        {
                            <umbnavitem menu-item="child" active-class="nav-link--active" class="nav-link"></umbnavitem>
                        }
                    </div>
                </li>
            }
            else
            {
                <li class="nav-item" >
                    <umbnavitem menu-item="item" active-class="nav-link--active" class="nav-link"></umbnavitem>
                </li>
            }
    
        </ul>
    }
    
  • Cedeste 37 posts 129 karma points
    Feb 02, 2023 @ 13:07
    Cedeste
    0

    Thank you!

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 02, 2023 @ 15:06
    Huw Reddick
    0

    Obviously, if you need more than 2 levels it will need expanding further, but just shout if you need more assistance.

  • Cedeste 37 posts 129 karma points
    Feb 02, 2023 @ 15:09
    Cedeste
    0

    Thank you. I'm starting to look at it now. I really appreciate your help.

  • Cedeste 37 posts 129 karma points
    Feb 02, 2023 @ 16:42
    Cedeste
    0

    Thank you again. I used your example and worked it in with what I was able to come up with last night. I've got it this far:

       @{
        var site = Model.Root();
        var umbNav = site.Value<IEnumerable<UmbNavItem>>("umbNav");
    }
    
    <ul class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container px-4 px-lg-5">
            <a class="navbar-brand" href="">Test Site</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                Menu
                <i class="fas fa-bars"></i>
            </button>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ms-auto py-4 py-lg-0">
                    @foreach (var navigationLink in umbNav)
                    {
                        @if (navigationLink.Children != null && navigationLink.Children.Any())
                        {
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle @navigationLink.CustomClasses @Html.If(navigationLink.IsActive, "active")" href="@navigationLink.Url()" target="@navigationLink.Target" role="button" data-bs-toggle="dropdown" aria-expanded="false">@navigationLink.Title</a>
    
                    <!-- sub menu start -->         
                               <ul class="dropdown-menu">
    
                                    <li>
                                        <a class="dropdown-item @navigationLink.CustomClasses" href="@navigationLink.Url()" target="@navigationLink.Target">Go to @navigationLink.Title</a>
    
    
                                @foreach (var child in navigationLink.Children)
                                    {
                                    <ul  class="dropdown-menu dropdown-submenu dropdown-submenu-left">
    
                                        <li>
                                            <a class="dropdown-item @child.CustomClasses" href="@child.Url()" target="@child.Target" >@child.Title</a>
                                            <ul  class="dropdown-menu dropdown-submenu dropdown-submenu-left">
    
                                        <li>
                                            <a class="dropdown-item @child.CustomClasses" href="@child.Url()" target="@child.Target">@child.Title</a>
                                            <ul  class="dropdown-menu dropdown-submenu dropdown-submenu-left">
                                        <li>
                                            <a class="dropdown-item @child.CustomClasses" href="@child.Url()" target="@child.Target">@child.Title</a>
                                        </li>
    
                                        </ul>
                                        </li>
                                        </ul>
                                        </li>
                                        </ul>
                                    }       
    
    
    
                                    </li>
    
    
                                </ul>
    
                    <!-- sub menu end -->           
    
                           </li>
                        }
                        else
                        {
                            <li class="nav-item">
                                <a class="nav-link @navigationLink.CustomClasses @Html.If(navigationLink.IsActive, "active") " href="@navigationLink.Url()" target="@navigationLink.Target">@navigationLink.Title</a>
                            </li>
                        }
                    }
                </ul>
                </div>
                </div>
                </ul>
    

    This is working successfully, but now I'm not sure how to add the dynamic values for the additional child links. I'm still researching, but im not sure what the call is for the children of children if that makes any sense.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 04, 2023 @ 07:47
    Huw Reddick
    0

    Hi Cedeste,

    This is working successfully, but now I'm not sure how to add the dynamic values for the additional child links.

    You just need to repeat what you have done for each level that you want to expand, so in your

    @foreach (var child in navigationLink.Children){
    
    ...}
    

    Section you would do another check for children

    @if(child.Children.Any() ... etc
    

    You could simplify it by using some razor functions/helpers

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 02, 2023 @ 09:11
    Huw Reddick
    0

    Hi Cedeste,

    The _TopNavigation code template is only designed to display a single level of the menu, you will need to add your own template if you require dropdowns/multilevel menu items.

    I'm not at my PC right now, so not able to post you an example, if nobody else chimes in I will post an example later today when I am back at my computer.

Please Sign in or register to post replies

Write your reply to:

Draft