Copied to clipboard

Flag this post as spam?

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


  • Brian McNally 16 posts 104 karma points
    Aug 06, 2014 @ 18:23
    Brian McNally
    0

    Determine of Current page is Descendant of Node ID

    Another syntax issue I just can't figure out. Seems like it should be pretty simple.

    I have a hard-coded set of 3 links on our site on the top of every page. The first two links are for sections of the site and the third links out to another external site.

    I'm just needing to set an "active" class based on if the current page exists under a particular node.

    So the working Link list would look something like this:

    <ul>
    <li class="active"><a href="/">Main Section</a></li>
    <li><a href="/SubSection/">Sub Section</a></li>
    <li><a href="http://ExternalURL.com">External link</a></li>
    </ul>
    

    The site structure has the "Sub Section" as a node beneath the "Main Section", with many nodes beneath each. I'd like to be able to say..If the current page exists under node ID of the 'SubSection'..say node ID 1020 for example..then set the sunsection list item link to active, otherwise set the 'main section' link to active.

    This is on an MVC Umbraco 7 setup. The mini nav list has been setup as it's own MVC Partial View Template and is being called like this:

     @Html.Partial("TopMiniNav", Model.Content)
    

    I've tried quite a few different ways and no luck yet and feel like I'm getting further from the solution. I just don't want to build out different templates with the active css hardcoded in just because I can't figure it out :-). whenever I try any of the ancestors or desendants I just get an error:

    'Umbraco.Web.Models.RenderModel' does not contain a definition for 'AncestorOrSelf' ..and so on..so not sure if I'm missing something obvious and I guess I'm just not clear on my setup.

    the top of my partial view file has this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    If that helps out.

    I've been able to build other 'sibling' navs on the site and set it up to style out the "active" page in the nav, but not sure how to handle this one..Any help or guidance would be great..Thanks and please let me know if I'm leaving out any pertinent info!

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 06, 2014 @ 18:50
    Nicholas Westby
    101

    Are you trying to use that as a property, but it's really a function? Hard to know without seeing your code (and what Umbraco version are you using?). Also, what about using the plural version ("ancestors" rather than "ancestor")? I don't have Visual Studio open right now, but maybe something like this would work:

    var active = Model.Content.AncestorsOrSelf().Select(x => x.Id).ToArray().Contains(1020);
    

    Alternatively, there appears to be an IsAncestorOrSelf function: https://github.com/umbraco/Umbraco-CMS/blob/8a1db41fc44493380624b89e49372a36248f3757/src/Umbraco.Web/PublishedContentExtensions.cs#L871

    If you only have the node ID (e.g., 1020), you can use Umbraco.TypedContent to get the IPublishedContent for that node ID so you can use the IsAncestorOrSelf function.

  • Brian McNally 16 posts 104 karma points
    Aug 06, 2014 @ 19:04
    Brian McNally
    0

    Thanks so much Nicholas..That did the trick!

Please Sign in or register to post replies

Write your reply to:

Draft