i'm trying to highlight the current link in MVC 4 but i cannot do this, i'm sharing what i have done so far
MenuHelper Extension:
public static class MenuHelper
{
public static MvcHtmlString ListItemAction(this HtmlHelper helper, string name, string actionName, string controllerName)
{
var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
var sb = new StringBuilder();
sb.AppendFormat("<li{0}", (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) &&
currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase)
? " class=\"active\">" : ">"));
var url = new UrlHelper(HttpContext.Current.Request.RequestContext);
sb.AppendFormat("<a href=\"{0}\">{1}</a>", url.Action(actionName, controllerName), name);
sb.Append("</li>");
return new MvcHtmlString(sb.ToString());
}
}
THis is my HTML
<li class="has-sub active">
<a href="javascript:;" class="">
<i class="icon-magnet"></i>Manage
<span class="arrow open"></span>
<span class="selected"></span>
</a>
<ul class="sub">
@{
@Html.ListItemAction("Parts", "Index", "Part")
@Html.ListItemAction("Categories", "Index", "Category")
@Html.ListItemAction("Tickets", "Index", "Ticket")
@Html.ListItemAction("SearchLog", "Index", "SearchLog")
@Html.ListItemAction("Reviews", "Index", "Review")
}
</ul>
</li>
this is just highlighting the link but i want to change the LI tag class as well because of my design. i have other links as well i want that when the link is clicked the parent LI tag will be assigned with the active class.how i can do this? i have searched internet but couldn't get any method to solve this.
Here is my menu helper which includes nested links so it highlight both active link and parent menu item. I think you can modify it for your solution.