简体   繁体   中英

Umbraco 7 Razor - find parent

I'm using Umbraco 7 and I want a partial view to render a left nav starting at a certain point up the tree where it finds a page with a certain property set.

I've tried this but it only works from a fixed depth 1 layer down in the tree. I want to do this from any depth within the site and have it recurse back up until it finds an item and have that be the root for the left nav.

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
    @* Get the item above this one that has a property of resetLeftNav ticked *@
    var root = CurrentPage.AncestorOrSelf(1).Children.Where("resetLeftNav").First(); 
}

<ul class="nav left-nav">
    @foreach(var page in root.Children)
    {
        <li class="">
            <a href="@page.Url">@page.Name</a>
        </li>
    }
</ul>

You could use:

var root = Model.Content.AncestorsOrSelf().First(
                 x => x.GetPropertyValue<bool>("resetLeftNav")
              );

The only difference is that you were looking for a single ancestor. Mine looks for them all which allows me to do the check and then take the first one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM