0

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?

ekad
  • 14,436
  • 26
  • 44
  • 46
Denis Wasilew
  • 503
  • 3
  • 9
  • 29
  • What is `class="active"`? And what do you mean there is no controller? – Bhushan Shah Aug 05 '16 at 14:07
  • Did you try javascript? – Berkay Yaylacı Aug 05 '16 at 14:09
  • Umm... It's just css class to highlight the link of the page that user is currently on. There is obviously no controller that returns the partial, because.... it's a partial. Didn't tried js. More details please? – Denis Wasilew Aug 05 '16 at 14:10
  • 1
    Check [this](http://jsfiddle.net/rq9UB/). – Berkay Yaylacı Aug 05 '16 at 14:13
  • Oh it's pretty nice, but there is one issue. If user is on another page, and refreshes it, the partial is also getting refreshed - so the tag dissapears and user won't know which is the current site, since js removed the color tag after refresh. – Denis Wasilew Aug 05 '16 at 14:17
  • Possible duplicate of [How to add "active" class to Html.ActionLink in ASP.NET MVC](http://stackoverflow.com/questions/20410623/how-to-add-active-class-to-html-actionlink-in-asp-net-mvc) – dom Aug 05 '16 at 15:01

1 Answers1

2

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>
 ..................
Alex Suleap
  • 559
  • 4
  • 12