ASP.NET MVC - Highlight currently active menu item

This particular problem was haunting me quite a some time now. Most of the times, I've ignored it and never showed what the currently active menu item is...Until the client specifically requests for it.

After going through some blogs and StackOverFlow questions, I've found these various solutions.

Setting an active menu item based on the current URL with jQuery
In short, it uses jQuery to match the url with link "href" value and set the active class if matches.
And then there's this MVC4 related way, which could get ugly if there are a lot of menu items.

<li class="@( RouteVariable.CurrentController.Equals("Home") ? "active" : "")">

And, another way is to create a HTML Helper method. Then you can do this, which is ok but to make it more flexible, you might have to code a lot.


<ul>
    @Html.MenuItem("Home", "Home", "Home")
    @Html.MenuItem("Products", "Index", "Products")
</ul>

This blog also defines a Helper Method. and this too, also this one.

Popular posts from this blog

Print a receipt using a Thermal Printer with C#.NET

Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Complex Master-Detail Form using Knockout.js and ASP.NET MVC