在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
扩展。
编辑于03-24-2015:不得不重写此方法,以允许多个操作和控制器触发所选行为,并处理从子操作部分视图调用该方法的情况,我想分享更新!
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>
active
类添加到li
元素而不是a
。请参见此处的第一个示例:http://getbootstrap.com/components/#navbar - dom