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:
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!
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);
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.
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:
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:
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:
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!
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:
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.
Thanks so much Nicholas..That did the trick!
is working on a reply...