I would like to mark the current node and parent with the css class. I searched and found these links:
http://mvcsitemap.codeplex.com/discussions/257786 http://mvcsitemap.codeplex.com/discussions/245000
So, I changed SiteMapNodeModelList.cshtml and now the current node is highlighted. But I don’t know how I should allocate the parent node.
<ul> @foreach (var node in Model) { <li class="@(node.IsCurrentNode ? "current" : "")" >@Html.DisplayFor(m => node) @if (node.Children.Any()) { @Html.DisplayFor(m => node.Children) } </li> } </ul>
To mark the parent node, I built an extension method that checks all immediate children (I only have 2 levels):
public static bool IsCurrentNodeOrChild(this SiteMapNodeModel node) { if (node.IsCurrentNode) return true; return node.Children.Any(n => n.IsCurrentNode); }
And changed MenuHelperModel.cshtml as follows:
<ul id="menu"> @foreach (var node in Model.Nodes) { <li class="@(node.IsCurrentNodeOrChild() ? "current" : "d")" >@Html.DisplayFor(m => node) @if (node.Children.Any()) { @Html.DisplayFor(m => node.Children) } </li> } </ul>
Now it works fine. But is there really no simpler approach? Can't be the first person on earth to need this?
source share