Question

i am very much new in Umbraco CMS and started coding in it recently. My page structure is like thisenter image description here

And now I have written the code like this to generate the dynamic navigation based on the code I have found in Umbraco forums. But in output it is giving only the 'Home' as the link not other links.The reason is that I have written it like this { var homeNode = Model.AncestorOrSelf(1); } and it is only returning home page not other pages. i have tried using this also Model.AncestorOrSelf(). But it is giving same result.

<nav>
    <ul>

        @{ var homeNode = Model.AncestorOrSelf(1); }

        <li><a href="@homeNode.Url" class="@Library.If(homeNode.Id == Model.Id, "selected", "")">@homeNode.Name</a></li>

        @foreach (var page in Model.Children.Where("Visible"))
        {
            var isSelected = false;
            if (Model.Id == page.Id || (Model.Parent != null && Model.Parent.Id == page.Id && Model.NodeTypeAlias != "Textpage"))
            {
                isSelected = true;
            }

            <li>
                <a href="@page.Url" class="@Library.If(isSelected, "selected", "")">@page.Name</a>

                <!-- If the page has child nodes (2nd level) that are visible and docTypeAlias is Textpage (textpages) -->
                @if (page.Textpages.Where("Visible").Count() > 0)
                {
                    <ul>
                        @foreach (var childPage in page.Children.Where("Visible"))
                        {
                            <li><a href="@childPage.Url" class="@Library.If(childPage.Id == Model.Id, "selected", "")">@childPage.Name</a></li>
                        }
                    </ul>
                }
            </li>
        }
    </ul>
</nav>

What will be the proper expression to traverse this.

Thanks and Regards

utpal

Was it helpful?

Solution

Right so the first thing you want is to change your site hierarchy to this:

Home
|-- About Us
|-- Contact Us
|-- Services

You can find in Document Types, under Settings in the home node'S document type, the Structure tab to add the allowed node children node types to make the home node have allowed children of the 3 types you have.

Next you'll want this code to reach your home node:

var homeNode = Model.Content.AncestorOrSelf("Site");

Where "Site" is your home node's document type.

Next you can prepare the data you'll want to loop:

var menuItems = homeNode.children.Where(x => x.IsVisible());

Finally you can change your loop to this:

@foreach (var page in menuItems)

This should help you get this to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top