I'm using MVC.net, having difficulty to get through bootstrap sidebar li stay active after click and continue to refresh the page all the time i click.
However i have try to use the example provide below still, it still can't be active after click.
i have try through several answer from the link Click option1 here and Click option2 here
Below is my code:
CurrentControllerActiveClassName:
public string CurrentControllerActiveClassName(string targetController)
{
var controller = ViewContext.RouteData.Values["controller"];
return controller.Equals(targetController) ? "active" : string.Empty;
}
Html layout:
<ul class="sub-menu" style="display: block;">
<li class="dropdown @CurrentControllerActiveClassName("Option")">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">@Manager.Resource.Value("ABC000") </a>
<ul class="sub-menu">
<li>@Html.ActionLink(Manager.Resource.Value("ABC001"), "Import", "Others Country")</li>
<li>@Html.ActionLink(Manager.Resource.Value("ABC002"), "Apply", "Accept")</li>
<li>@Html.ActionLink(Manager.Resource.Value("ABC003"), "Terminate", "Stop Purchasing")</li>
</ul>
</li>
</ul>
Jquery:
$('li:has(ul)', '#sidebar-content ul').each(function() {
if ($(this).hasClass('current') || $(this).hasClass('open-default')) {
$('>a', this).append("<i class='arrow " + arrow_class_open + "'></i>");
} else {
$('>a', this).append("<i class='arrow " + arrow_class_closed + "'></i>");
}
});
if ($('#sidebar').hasClass('sidebar-fixed')) {
$('#sidebar-content').append('<div class="fill-nav-space"></div>');
}
$('#sidebar-content ul > li > a').on('click', function (e) {
var navItem = $(this); //add to try
if (navItem.find("a").attr("href") == location.pathname) { //add to try
navItem.addClass("active"); //add to try
}
if ($(this).next().hasClass('sub-menu') == false) {
return;
}
The result should be stay active while sidebar is click.