I have the following menu:
<ul class="nav nav-tabs nav-stacked">
<li class="nav-header">Navigation Menu</li>
<li>@Html.MenuLink("Test Link", "Index", "Home", "active",true)</li>
MenuLink is a helper that sets a class to the ActionLink (href) element:
public static HtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string activeClass, bool checkAction)
{
string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
if (string.Compare(controllerName, currentController, StringComparison.OrdinalIgnoreCase) == 0 && ((!checkAction) || string.Compare(actionName, currentAction, StringComparison.OrdinalIgnoreCase) == 0))
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = activeClass });
}
return htmlHelper.ActionLink(linkText, actionName, controllerName);
}
What I need is to set the class attribute to the parent HTML element, in this case the <li>
element, so the final result would be:
<ul class="nav nav-tabs nav-stacked">
<li class="nav-header">Navigation Menu</li>
<li class="active"><href="....."></li>
instead of the actual result:
<ul class="nav nav-tabs nav-stacked">
<li class="nav-header">Navigation Menu</li>
<li><href="....." class="active"></li>
Any clue, any advise is appreciated.
Thanks.