Get a property on a specific content node using Model
I'm on Umbraco 7.7.8
I have the Meganav on a specific content node, and would like to reference it onto a template so that other content nodes use it. What would be the easiest way to reference it via model in razor?
The property alias for the meganav is TopMenu and the content node id that dresses it up is 1064
The following seems to make sense if I want to reference the property from the root location...
var mmeganav = Model.Content.Site().GetPropertyValue<IEnumerable<MeganavItem>>("topMenu");
Sure you can do that but tying you code to a specific nodeID is a bad idea.
A better method using Razor would be to traverse the tree to look for your mega nav type alias. This way you can change the node of the mega nav but as long as you type alias remains the same it will return a reliable collection of nodes. It also makes your code portable.
var megaNav = Umbraco.TypedContentSingleAtXPath("//megaNavTypeAlias");
Thanks for the good tip! I have not explored this yet. But to help me understand, does this process look for the first match of a property alias within the content tree? does it look through the whole content tree or just at root level?
The reason I am asking is because my Meganav property alias is not sitting on the root of my content tree, it's one level deep in my content tree.
It will traverse your tree looking for a type alias and return the first one it finds.
With navigation, you may typically have 2-3 nav structures (unless you are evaluating your tree as your nav). Lets say, Mega Nav, Subnav and may be a separate Mobile Nav.
Give these different type alias (just make a child node from your nav node (in doc types) and inherit all the properties). That way you really can't go wrong. Even if you move or copy the nav nodes the alias will remain bound.
This is a snippet from a Profile Helper class, which returns property values from document type called "siteLanguage". This runs from anywhere in the site.
Get a property on a specific content node using Model
I'm on Umbraco 7.7.8
I have the Meganav on a specific content node, and would like to reference it onto a template so that other content nodes use it. What would be the easiest way to reference it via model in razor?
The property alias for the meganav is
TopMenu
and the content node id that dresses it up is1064
The following seems to make sense if I want to reference the property from the root location...
But how would I modify this?
Thanks
I got it...
Works like a charm
Sure you can do that but tying you code to a specific nodeID is a bad idea.
A better method using Razor would be to traverse the tree to look for your mega nav type alias. This way you can change the node of the mega nav but as long as you type alias remains the same it will return a reliable collection of nodes. It also makes your code portable.
Thanks for the good tip! I have not explored this yet. But to help me understand, does this process look for the first match of a property alias within the content tree? does it look through the whole content tree or just at root level?
The reason I am asking is because my Meganav property alias is not sitting on the root of my content tree, it's one level deep in my content tree.
Many Thanks!
It will traverse your tree looking for a type alias and return the first one it finds.
With navigation, you may typically have 2-3 nav structures (unless you are evaluating your tree as your nav). Lets say, Mega Nav, Subnav and may be a separate Mobile Nav.
Give these different type alias (just make a child node from your nav node (in doc types) and inherit all the properties). That way you really can't go wrong. Even if you move or copy the nav nodes the alias will remain bound.
I get the concept, and read the docs on this, but still stuck on using it. On my view I have the following code...
This code returns the following error...
with the following highlighted in red:
Yet...this works....
What am I missing from my XPath logic?
OK i'll give you an example:
This is a snippet from a Profile Helper class, which returns property values from document type called "siteLanguage". This runs from anywhere in the site.
is working on a reply...