Denis Wasilew Denis Wasilew - 1 year ago 62
ASP.NET (C#) Question

Navigation bar as a partial view. What about "active" class

I've got a navigation bar as a partial view - it contains page links. Everything works fine, but how do I use the

<li class="active">
now? Since there is no controller, I can't use the
active
class to highlight on which page user is currently on. Any ideas?

Answer Source

To detect on which page you should set the active class you can check the ViewContext.RouteData. Ex:

<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Action1" ? "active" : "")">@Html.ActionLink("Action1", "Action1", "Home")</li>
 ..................