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

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

如何在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>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4892962.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存