I'm trying to add an"active" class to my bootstrap navbar in MVC, but the following doesn't show the active class when written like this:
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home", null, new {@class="active"})</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
This resolves to what looks like a correctly formatted class, but doesn't work:
<a class="active" href="/">Home</a>
In the Bootstrap documentation it states that 'a' tags shouldn't be used in the navbar, but the above is how I believe is the correct way of adding a class to an Html.ActionLink. Is there another (tidy) way I can do this?
This solution is simple for Asp.net MCV 5.
Create a static class, for example
Utilitarios.cs
.Inside the Class create a static method:
call like this
I modified dom's "not pretty" answer and made it uglier. Sometimes two controllers have the conflicting action names (i.e. Index) so I do this:
May be little late. But hope this helps.
And usage as follow:
Got reference from http://www.codingeverything.com/2014/05/mvcbootstrapactivenavbar.html