栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在ASP.NET MVC中将“活动”类添加到Html.ActionLink

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在ASP.NET MVC中将“活动”类添加到Html.ActionLink

在Bootstrap中,

active
需要将类应用于
<li>
元素,而不是
<a>

基于活动状态或不活动状态处理UI样式的方式与ASP.NETMVC的

Actionlink
帮助程序无关。这是遵循Bootstrap框架构建方式的正确解决方案。

<ul >    <li >@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 >    <li Action"].ToString() == "Index" ? "active" : "")">@Html.Actionlink("Home", "Index", "Home")</li>    <li Action"].ToString() == "about" ? "active" : "")">@Html.Actionlink("about", "about", "Home")</li>    <li Action"].ToString() == "Contact" ? "active" : "")">@Html.Actionlink("Contact", "Contact", "Home")</li></ul>

它不是很漂亮的代码,但是可以完成工作,并允许您根据需要将菜单提取到局部视图中。有多种方法可以使这种方法更加简洁,但是由于您刚刚入门,因此我将其保留下来。祝您学习ASP.NETMVC一切顺利!


后期编辑:

这个问题似乎有点流量,所以我想我会使用

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 Home", controllers: "Default")">        <a href="@Url.Action("Home", "Default")">Home</a>    </li>    <li List,Detail", controllers: "Default")">        <a href="@Url.Action("List", "Default")">List</a>    </li></ul>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号