Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Dec 01, 2017 @ 23:54
    blackhawk
    0

    How to render a 2nd level for MegaNav with Models Builder

    The following code works for me in rendering a single level mega menu with Models Builder...

        @{
            if (Model.HasValue("MytopNav"))
            {
                <ul>
                    @foreach (var item in Model.MytopNav)
                    {
                        <li class="dropdown">
                        @item.Title
                          @*check for child links here*@
                        </li>
                    }
                </ul>
            }
        } 
    

    But if any of my parent level li's have child links, how do I check for that and add it to this logic?

  • Ben Palmer 180 posts 866 karma points c-trib
    Dec 02, 2017 @ 09:29
    Ben Palmer
    100

    Hi blackhawk,

    You can just check for children in each loop and then do another loop inside, something like this:

    @{
        if (Model.Content.HasValue("MytopNav"))
        {
            <ul>
                @foreach (var item in Model.MyTopNav)
                {
                    <li class="dropdown">
                        @item.Title
                        @*check for child links here*@
    
                        @if (item.Children.Any())
                        {
                            <ul>
                                @foreach (var child in item.Children)
                                {
                                    <li>@child.Title</li>
                                }
                            </ul>
                        }
                    </li>
                }
            </ul>
        }
    }
    
  • blackhawk 313 posts 1368 karma points
    Dec 02, 2017 @ 13:04
    blackhawk
    1

    Thanks for the help! I only had to change Children() to Children.

  • Ben Palmer 180 posts 866 karma points c-trib
    Dec 02, 2017 @ 16:20
    Ben Palmer
    0

    Ah, so close! I had thought it was that actually but my Visual Studio told me otherwise. Strange, updated my answer to match however.

  • ibrahim TUNC 55 posts 133 karma points
    Feb 22, 2018 @ 17:13
    ibrahim TUNC
    0

    How to render third and fourth levels for MegaNav with Models Builder?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies