Basically AncestorOrSelf() has a number of overloads, one of which takes a a NodeTypeAlias parameter, which will only returns nodes of that type. In this case it will walk up the tree until it finds the first node with an alias of Region.
If you are using Visual Studio to edit your Razor files then a tip I use is to type @CurrentModel instead of @Model and you should get intellisense that displays all the methods availale on DynamicNode (which is the actual type of Model). But if you want to reference custom properties you still need to use @Model as these are only available on dynamic types.
Finding the first parent of a certain type
Hello,
If I wanted to traverse up the tree in a razor script until I found a parent node of the type Region, how would I do this?
My solution works, but not if there are several Regions at the same level:
Maybe with a Where filter?
Haven't tested this-- it's just an idea.
Thanks Keith,
That would correctly get regions, but would encounter the same issue with not getting the parent of the current node.
I may be missing what you are trying to do, but this sounds like what you need:
That worked out perfectly! Thanks. Can you explain why it works that way? I can't find documentation anywhere.
Basically AncestorOrSelf() has a number of overloads, one of which takes a a NodeTypeAlias parameter, which will only returns nodes of that type. In this case it will walk up the tree until it finds the first node with an alias of Region.
Proper Razor documentation is pretty thin, as you noted. The best place to find some is at http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets
If you are using Visual Studio to edit your Razor files then a tip I use is to type @CurrentModel instead of @Model and you should get intellisense that displays all the methods availale on DynamicNode (which is the actual type of Model). But if you want to reference custom properties you still need to use @Model as these are only available on dynamic types.
Thanks for clarifying, I appriciate it.
These projects are also helpful for documentation and examples:
http://our.umbraco.org/projects/developer-tools/cultiv-razor-examples
http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet
is working on a reply...