1

I have layout page in asp.net MVC application and this page contains a navbar

 <ul class="sidenavbar">
   <li><a   style="font-size:large">ASP.NET Tutorial</a></li>
   <li>@Html.ActionLink("Home","Home")</li>
   <li>@Html.ActionLink("Introduction", "Introduction")</li>
   <li>@Html.ActionLink("Getting Started", "GettingStarted")</li>
 </ul>

See I have actionmethod for each item in my navbar now I want to apply css class active to currently selected list item how can I achieve this using ViewContext or if there is any better way?

voromax
  • 3,369
  • 2
  • 30
  • 53
Waqar Ul Khaf
  • 569
  • 1
  • 3
  • 15
  • 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) – Denys Wessels Aug 20 '16 at 15:54

1 Answers1

0

This may work

<ul class="nav navbar-nav">
  <li class="@(ViewContext.RouteData.Values["controller"].ToString() == "Home" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>
  <li class="@(ViewContext.RouteData.Values["controller"].ToString() == "About" ? "active" : "")">@Html.ActionLink("About", "About", "Home")</li>
  <li class="@(ViewContext.RouteData.Values["controller"].ToString() == "Contact" ? "active" : "")">@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
  • can i access ViewContext.RouteData in a c# class actually i want to make an extention method similar to ActionLink() that does this job in addition? – Waqar Ul Khaf Aug 20 '16 at 14:49