Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I consider myself an expert in rendering navigations using XSLT, and using Razor I've come quite far as well - as long as they're based on the Content structure it's no problem.
Now I have a site which is using Cogworks' MegaNav package for the navigation, and here's where I get stuck:
I need to add a class on a top level item if the currently rendered page is somewhere below it. Easy when using the content structure, but when using MegaNav, I don't have access to any Ancestor/Descendant helper methods that works within the structure built by the editor. At least not as far as I can tell...
How do you handle this scenario (if at all)?
I have not used MegaNav myself, but it seems like the MeganavItem class has a Content property, where the node of the item is stored (if not external).
So assuming you do something like this:
@foreach (var item in Model.Content.GetPropertyValue<IEnumerable<MeganavItem>("mainNavigation")))
You should be able to test for ancestors like (Model is the current page rendered)
if (item.Content != null && item.Content.IsAncestorOf(Model))
// something to do if current page is in the path of item
Brilliant, Søren — had forgot about the Is* helpers, actually.
Will try it out right away.
Ah, but no :(
I forgot the original problem again: Because the Meganav structure can be totally different from the actual content structure, I really can't use IsAncestorOf() etc.
Even with a simple content structure like this:
I can create a Meganav structure that looks like this:
- Who we are (link to /about/)
- All of them (link to /products/)
When rendering the Products page, I need to set a .currentpath class (or similar) on the Our products item and I can't get that info without traversing the actual Meganav model...
How about this?
if (item.Children.SelectRecursive(x => x.Children).Select(x => x.Id).Contains(Model.Id)
This one gets all descendants id's of item, and then checks if the current node's id (Model.Id) is one of them.
Never heard of SelectRecursive() before - but it's perfect here :)
Thanks a ton!
(Thankfully, the Meganav allows setting a max. level, so I don't think there will be any performance concerns with this.)
Haha, I never used it before - but Intellisense is your friend :)
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted