views:

19

answers:

2

Hi, i'm having issues implenting a dynamic action-based menu system in a PartialView as below:

<ul class="tabs">
    <li <% if (ViewContext.RouteData.Values["Action"] == "Index"){ %>class="active"<% } %>><a href="/talent">Home</a></li>
    <li <% if (ViewContext.RouteData.Values["Action"] == "account"){ %>class="active"<% } %>><a href="/talent/account">Account settings</a></li>
    <li <% if (ViewContext.RouteData.Values["Action"] == "password"){ %>class="active"<% } %>><a href="/talent/password">Password settings</a></li>
    <li <% if (ViewContext.RouteData.Values["Action"] == "profile"){ %>class="active"<% } %>><a href="/talent/profile">Edit profile</a></li>
    <li <% if (ViewContext.RouteData.Values["Action"] == "messages"){ %>class="active"<% } %>><a href="/talent/messages">Messages</a></li>
    <li id="logout"><a href="/logout">Logout</a></li>
</ul>

The problem is that only the 'Index' menu item works, where the CSS class is rendered in the HTML markup. The others don't, really strange. Any ideas??

+1  A: 

All action methods other than Index specified with lowcase. Use case insensitive comparison instead of "=="

Koistya Navin
@Koistya thanks. Fixed it using your wisdom the answer below
shahid81
+1  A: 
<% string action = ViewContext.RouteData.Values["Action"].ToString().ToLower(); %>

<ul class="tabs">
    <li <% if (action == "index"){ %>class="active"<% } %>><a href="/talent">Home</a></li>
    <li <% if (action == "account"){ %>class="active"<% } %>><a href="/talent/account">Account settings</a></li>
    <li <% if (action == "password"){ %>class="active"<% } %>><a href="/talent/password">Password settings</a></li>
    <li <% if (action == "profile"){ %>class="active"<% } %>><a href="/talent/profile">Edit profile</a></li>
    <li <% if (action == "messages"){ %>class="active"<% } %>><a href="/talent/messages">Messages</a></li>
    <li id="logout"><a href="/logout">Logout</a></li>
</ul>
shahid81
Looks good. For more advanced scenario where you need a button highlighted on subpages as well, you may want to create Html or Url helper which could be used like this <%= Url.Highlight("/account/*", "active") %>
Koistya Navin