基于动态操作的URL菜单问题c#mvc

时间:2010-08-29 22:20:53

标签: c# asp.net-mvc menu partial-views

我遇到了在PartialView中实现基于动态操作的菜单系统的问题,如下所示:

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

问题是只有'Index'菜单项有效,CSS类在HTML标记中呈现。其他人没有,真的很奇怪。任何想法??

2 个答案:

答案 0 :(得分:1)

除了使用lowcase指定的索引以外的所有操作方法。使用不区分大小写的比较而不是“==”

答案 1 :(得分:1)

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