Learner
Learner

Reputation: 351

MVC4 How can I set li class to active when the view is active

Hi all I am having my _layout as follows

<div id="primary_nav">
    <ul>
        <li class="left active" id="nav_discussion" runat="server">
          <a title="Go to Forums" href="@Url.Action("Index", "Home")">Forums</a>
       </li>
       <li class="left" id="nav_members" runat="server">
          <atitle="Go to Member List" href="@Url.Action("Members", "Home")">Members</a>
       </li>
    </ul>
</div>

This I used as a layout or master page for every view I created, now what I required is When I moved to ...Home/Members I would like to set Members tab as active like

enter image description here

Upvotes: 3

Views: 5940

Answers (2)

Venkata K. C. Tata
Venkata K. C. Tata

Reputation: 5547

I would suggest u to on clicking members use Jquery to remove class "left active" from forms and add it to Members .. that would be an easy solution!!!

Upvotes: -2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039160

I would recommend you writing a custom helper for that. I have illustrated an example of how this could be achieved here: https://stackoverflow.com/a/6323032/29407

You could of course write some spaghetti code in your _Layout (as you have already started) and test the current controller and action using the RouteData and test whether it equals to the specified value and apply some custom CSS class to make it active. I prefer not to show an example of that because I consider it really bad practice. Encapsulating your menu items in a reusable helper is what I would do.

Upvotes: 5

Related Questions