I would like to mark the current node and it's parent with a css class. I was searching around and found these links:
http://mvcsitemap.codeplex.com/discussions/257786 http://mvcsitemap.codeplex.com/discussions/245000
So I modified SiteMapNodeModelList.cshtml and now the current node is highlighted. But not sure how I should highlight 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've built an extension method that checks all immediate child elements (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 like this:
<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>
This now works perfectly. But is there really no simpler approach? Can't be the first man on earth that needs this?