我正在尝试在 MVC 中向我的 Bootstrap 导航栏添加一个“事件”类,但如下所示不显示事件类:
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home", null, new {@class="active"})</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
这解析为看起来格式正确的类,但不起作用:
<a class="active" href="/">Home</a>
在 Bootstrap 文档中,它指出不应在导航栏中使用“a”标签,但我认为以上是将类添加到 Html.ActionLink 的正确方法。还有另一种(整洁的)方法可以做到这一点吗?
最佳答案
在 Bootstrap 中 active
类需要应用于 <li>
元素而不是 <a>
.请在此处查看第一个示例:http://getbootstrap.com/components/#navbar
根据事件或非事件来处理 UI 样式的方式与 ASP.NET MVC 的 ActionLink
无关。 helper 。这是遵循 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>
它的代码并不漂亮,但它可以完成工作,并允许您根据需要将菜单提取到局部 View 中。有一些方法可以以更简洁的方式执行此操作,但由于您才刚刚开始,所以我会保留它。祝你学习 ASP.NET MVC 好运!
后期编辑:
这个问题似乎引起了一些关注,所以我想我会使用 HtmlHelper
提出一个更优雅的解决方案。扩展名。
编辑 2015 年 3 月 24 日:必须重写此方法以允许多个操作和 Controller 触发选定的行为,以及处理从子操作部分 View 调用该方法的时间,我想我会分享更新!
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>
关于asp.net - 如何在 ASP.NET MVC 中将 "active"类添加到 Html.ActionLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410623/