I am using Bootstrap 3.2.0 to create a navbar in my MVC3 application. Also I am using jquery-1.11.1.js.
The problem I am facing is that my navbar is created properly and it's responsive too, but the "active" class is not retained on the selected navbar item once the new page loads.
What is happening is that when I click on the navbar item, selected item gets the "active" class but after the new page is loaded, selected item losses the "active" class and the "active" class goes to the first item in the nav list.
I am pasting the code below for reference:
_Layout.cshtml:
<!-- language: lang-html --><body>
<div class="container">
<div class="row" id="navigationbar"
style="height:50px; background-color:black;">
@Html.Partial("_navbar")
</div>
<div class="content">
......
</div>
</div>
</body>
_navbar.cshtml:
<!-- language: lang-html --><div class="navbar navbar-default col-xs-10" role="navigation"
style="background-color: orange; border: 0px; border-radius: 0px; text-align: center;">
<div>
<button type="button" class="navbar-header navbar-toggle" data-toggle="collapse"
data-target="#navbarcollapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="navbarcollapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/Home/Index">Home</a></li>
<li><a href="/Home/ListOfEmployees">Employee List</a></li>
<li><a href="/Register/Index">Registration</a></li>
<li><a href="/Home/AddEmployee">Add Employee</a></li>
</ul>
</div>
</div>
And I have included the below jquery code in the _navbar.cshtml:-
<!-- language: lang-js -->$(".nav a").on("click", function () {
$(".nav").find(".active").removeClass("active");
$(this).parent().addClass("active");
});