【发布时间】:2023-03-27 10:53:01
【问题描述】:
查看Orange Tabs ASP.NET MVC 演示如何处理选项卡后,他们有类似的内容:
查看:
<ul id="menu">
<% if (Html.IsCurrentAction("Index", "Home")) { %>
<li class="active"><%= Html.ActionLink("Home", "Index", "Home")%></li>
<% } else { %>
<li><%= Html.ActionLink("Home", "Index", "Home") %></li>
<% }%>
<% if (Html.IsCurrentAction("About", "Home"))
{ %>
<li class="active"><%= Html.ActionLink("About", "About", "Home")%></li>
<% } else { %>
<li><%= Html.ActionLink("About", "About", "Home")%></li>
<% }%>
<% if (Html.IsCurrentAction("SampleTags", "Home"))
{ %>
<li class="active"><%= Html.ActionLink("Sample Tags", "SampleTags", "Home")%></li>
<% } else { %>
<li><%= Html.ActionLink("Sample Tags", "SampleTags", "Home")%></li>
<% }%>
</ul>
以及相应的辅助类:
namespace Helpers
{
public static class IsCurrentActionHelper
{
public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
return true;
return false;
}
}
}
这似乎是解决这个问题的一种优雅方式吗?我见过几十种不同的方式,从 javascript 到查询字符串等等。
我不喜欢 javascript,因为我希望该网站适用于不支持 js 的浏览器,而且查询字符串方法看起来很笨拙。
【问题讨论】:
标签: asp.net-mvc navigation tabs