1、创建MvcHtmlExtension扩展类
public static class MvcHtmlExtension
{
public static MvcHtmlString MenuLink(this HtmlHelper helper, string text, string action, string controller)
{
var routeData = helper.ViewContext.RouteData.Values;
var currentController = routeData["controller"];
var currentAction = routeData["action"];
if (String.Equals(action, currentAction as string, StringComparison.OrdinalIgnoreCase) &&
String.Equals(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
{
return helper.ActionLink(text, action, controller, null, new { @class = "menua" });
}
return helper.ActionLink(text, action, controller);
}
}
2、前台cshtml使用
<div class="collapse navbar-collapse pull-right">
<ul class="nav navbar-nav">
<li>@Html.MenuLink("育儿宝典", "YuErBaoDian", "Home")</li>
<li>@Html.MenuLink("关于我们", "About", "Home")</li>
<li>@Html.MenuLink("联系我们", "Contact", "Home")</li>
</ul>
</div>
作者:Tommy
出处:https://www.cnblogs.com/XL-Tommy/
本文版权归作者和博客园所有,欢迎转载,转载请标明出处。
如果您觉得本篇博文对您有所收获,觉得还算用心,请点击右下角的 [推荐],谢谢!