Asp.Net Mvc highlighting current page link technique?

前端 未结 6 1160
误落风尘
误落风尘 2020-12-04 10:35

I need to highlight active link in the menu. My menu is in the master page by the way. I\'m looking for the best way to implement this? Any ideas?

6条回答
  •  温柔的废话
    2020-12-04 10:50

    I am always using this solution Html Part

      @Html.ListItemAction("Home Page","Index","Home") @Html.ListItemAction("Manage","Index","Home")

    Helper Part

     public static class ActiveLinkHelper
        { 
          public static MvcHtmlString ListItemAction(this HtmlHelper helper, string name, string actionName, string controllerName)
          {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
            var sb = new StringBuilder();
            sb.AppendFormat("" : ">"));
            var url = new UrlHelper(HttpContext.Current.Request.RequestContext);
            sb.AppendFormat("{1}", url.Action(actionName, controllerName), name);
            sb.Append("
  • "); return new MvcHtmlString(sb.ToString()); } }

提交回复
热议问题