I have a couple of list items in a shared _layout.cshtm
file (master page) in my MVC application.
something like:
<ul>
<li>Home</li>
<li>about</li>
<li>contact</li>
<li>blog</li>
</ul>
when the user is in a homepage, I want home li
item to have class selected
, like so:
<li class="selected">Home</li>
and so on. What is the best way to do this?
In regular asp.net website, I used to have a method in master page and call that method from child page but in MVC I am not sure what to do.
thanks.
Just wanted to share what i do:
I create folder
App_Code
and addCustomHelpers.cshtml
. In it i create something like this:Than in my MasterPage (_Layout.cshtml) i add this where i want my menu to apear:
And than in my view, just like i change my page title, i change my selected menu:
Got my idea from this tutorial: www.asp.net/mvc/videos/mvc-3/mvc-3-razor-helpers
You could write a custom helper method:
and then:
The helper check the current action and controller and if they match the one passed as arguments to the helper it appends the
selected
CSS class to theli
.