这似乎是在ASP.NET MVC中实现选项卡的好方法吗?

Kin*_*tor 2 navigation asp.net-mvc tabs

在查看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>
Run Code Online (Sandbox Code Playgroud)

和相应的助手类:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这看起来像是解决这个问题的优雅方式吗?我已经看到了从javascript到查询字符串等的几十种不同方式.

我不喜欢javascript,因为我希望网站适用于非js启用的浏览器,并且查询字符串方法似乎很笨拙.

Tal*_*joe 5

<% if (Html.IsCurrentAction("Index", "Home")) { %>
     <li class="active"><%= Html.ActionLink("Home", "Index", "Home")%></li>
<% } else { %>
     <li><%= Html.ActionLink("Home", "Index", "Home") %></li>
<% }%>
Run Code Online (Sandbox Code Playgroud)

我将所有这些隐藏在辅助方法背后:

<%= Html.Activelink("Index", "Home", "Home") %>
Run Code Online (Sandbox Code Playgroud)