3

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

Elvin Mammadov
  • 25,329
  • 11
  • 40
  • 82
Learner
  • 351
  • 7
  • 25
  • I'm not sure that you can do this if you are setting the active class in your _layout file. I think you would either have to define your menu as a parameterized partial view, or use JavaScript. – vansimke Feb 09 '13 at 11:46
  • Yes, you could because the View is rendered before the _Layout and all its properties are available. Could use ViewBag or @DarinDimitrov s answer – Dave Alperovich Feb 09 '13 at 16:09

2 Answers2

5

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-2

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!!!

Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35