Is the current page a descendant of a specific node id?

我们两清 提交于 2019-12-11 00:52:46

问题


How do I check using a razor template in Umbraco to determine if the current page is a descendant of a specific node? I'll be using a case statement.


回答1:


Not sure if those methods still work with the latest version of Umbraco, but with 4.7.1 and DynamicNode there use to be those methods:

@Model.AncestorOrSelf(string nodeTypeAlias)
@Model.AncestorOrSelf(int level)
@Model.AncestorOrSelf(Func<DynamicNode, bool> func)

and those Helper functions:

@Model.IsDescendant(DynamicNode[,valueIfTrue][,valueIfFalse])
@Model.IsDescendantOrSelf(DynamicNode[,valueIfTrue][,valueIfFalse])



回答2:


If you're using uComponents / uQuery from http://ucomponents.codeplex.com/ you can do something like:

var isChildOf = uQuery.GetCurrentNode().GetAncestorNodes().Where(n => n.NodeTypeAlias == "MyHomePage").First() != null;

(Note: I haven't tried this code, but have similar in production)




回答3:


A quick and dirty way of doing it would be to check the nodes Path property (I think @Model.Path should get it). This should contain either a comma separate string or an array of numbers (not sure which off the top of my head) of the path from the node back to the root of the site. You can check for your parent node in that property. Which would save on more expensive lookups of nodes using LINQ or uQuery.



来源:https://stackoverflow.com/questions/13754328/is-the-current-page-a-descendant-of-a-specific-node-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!