Copied to clipboard

Flag this post as spam?

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


  • syn-rg 282 posts 425 karma points
    Apr 20, 2012 @ 08:07
    syn-rg
    0

    Umbraco Version 5 Multi Level navigation Razor

    Hi I'm creating a website in version 5 and am very new to Razor.

    I want a multi level navigation, something that will display the first 3 levels in the content tree.

    Is anyone able to help me with this, I just need to gain a better understanding of Razor. Also I don't want the Homepage node as the variable. I just want a simple nav that can display all the items at level one, then nest levels 2 and 3 within it.

    I'm using the following following code from the version 5.0.1 install starter package:

    @inherits RenderViewPage
    @using Umbraco.Cms.Web;

    @{
    var Homepage = @DynamicModel;
    while (Homepage.ContentType.Alias != "homePage")
    {
    Homepage = Homepage.Parent;
    }
    }
    <ul class="dropdown">
    <!--<li><a href="@Homepage.Url">Home</a></li>-->
    @foreach (var item in Homepage.Children)
    {
    if (@item.CurrentTemplate != null)
    {
    var childName = item.Name ?? "(No name yet)";
    <li><a href="@item.Url">@childName </a></li>
    }
    }
    </ul>
  • Owen 123 posts 246 karma points
    Apr 20, 2012 @ 10:15
    Owen
    0

    This is a way to get what you want, but might not the best one:

    @{
        var topLevel = this.Model.AncestorContentOrSelf().Where(n => n.ParentContent() == null);
        <ul>
        @* level 1 *@
        @foreach (var item in topLevel)
        {
            <li>
                1- @item.Name
                <ul>
                    @foreach (var level2Item in item.ChildContent())
                    {
                        <li>
                           2- @level2Item.Name
                            <ul>
                                @foreach (var level3Item in level2Item.ChildContent())
                                {
                                    <li>
                                       3 - @level3Item.Name
                                    </li>
                                }
                            </ul>
                        </li>   
                    }
                </ul>
            </li>   
        }
        </ul>
    }
Please Sign in or register to post replies

Write your reply to:

Draft