在 Bootstrap 中,active 类需要应用于<li> 元素,而不是<a>。请参阅此处的第一个示例:http://getbootstrap.com/components/#navbar
根据活动或非活动来处理 UI 样式的方式与 ASP.NET MVC 的 ActionLink 帮助器无关。这是遵循 Bootstrap 框架构建方式的正确解决方案。
<ul class="nav navbar-nav">
<li class="active">@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
编辑:
由于您很可能会在多个页面上重复使用您的菜单,因此最好有办法根据当前页面自动应用所选的类,而不是多次复制菜单并手动执行。
最简单的方法是简单地使用ViewContext.RouteData 中包含的值,即Action 和Controller 值。我们可以在您目前拥有的基础上构建类似这样的内容:
<ul class="nav navbar-nav">
<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Index" ? "active" : "")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "About" ? "active" : "")">@Html.ActionLink("About", "About", "Home")</li>
<li class="@(ViewContext.RouteData.Values["Action"].ToString() == "Contact" ? "active" : "")">@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
它的代码并不漂亮,但它可以完成工作并允许您根据需要将菜单提取到局部视图中。有一些方法可以以更简洁的方式做到这一点,但既然你才刚刚开始,我就先把它留在那里。祝您学习 ASP.NET MVC 好运!
后期编辑:
这个问题似乎有点流量,所以我想我会使用HtmlHelper 扩展提出一个更优雅的解决方案。
编辑 2015 年 3 月 24 日:必须重写此方法以允许多个动作和控制器触发所选行为,以及处理从子动作局部视图调用该方法的时间,我想我会分享更新!
public static string IsSelected(this HtmlHelper html, string controllers = "", string actions = "", string cssClass = "selected")
{
ViewContext viewContext = html.ViewContext;
bool isChildAction = viewContext.Controller.ControllerContext.IsChildAction;
if (isChildAction)
viewContext = html.ViewContext.ParentActionViewContext;
RouteValueDictionary routeValues = viewContext.RouteData.Values;
string currentAction = routeValues["action"].ToString();
string currentController = routeValues["controller"].ToString();
if (String.IsNullOrEmpty(actions))
actions = currentAction;
if (String.IsNullOrEmpty(controllers))
controllers = currentController;
string[] acceptedActions = actions.Trim().Split(',').Distinct().ToArray();
string[] acceptedControllers = controllers.Trim().Split(',').Distinct().ToArray();
return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
cssClass : String.Empty;
}
适用于 .NET Core:
public static string IsSelected(this IHtmlHelper htmlHelper, string controllers, string actions, string cssClass = "selected")
{
string currentAction = htmlHelper.ViewContext.RouteData.Values["action"] as string;
string currentController = htmlHelper.ViewContext.RouteData.Values["controller"] as string;
IEnumerable<string> acceptedActions = (actions ?? currentAction).Split(',');
IEnumerable<string> acceptedControllers = (controllers ?? currentController).Split(',');
return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
cssClass : String.Empty;
}
示例用法:
<ul>
<li class="@Html.IsSelected(actions: "Home", controllers: "Default")">
<a href="@Url.Action("Home", "Default")">Home</a>
</li>
<li class="@Html.IsSelected(actions: "List,Detail", controllers: "Default")">
<a href="@Url.Action("List", "Default")">List</a>
</li>
</ul>