ASP.NET MVC高亮顯示當前頁面菜單


1、創建MvcHtmlExtension擴展類

 1  public static class MvcHtmlExtension
 2     {
 3         public static MvcHtmlString MenuLink(this HtmlHelper helper, string text, string action, string controller)
 4         {
 5             var routeData = helper.ViewContext.RouteData.Values;
 6             var currentController = routeData["controller"];
 7             var currentAction = routeData["action"];
 8             if (String.Equals(action, currentAction as string, StringComparison.OrdinalIgnoreCase) &&
 9                 String.Equals(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
10             {
11                 return helper.ActionLink(text, action, controller, null, new { @class = "menua" });
12             }
13             return helper.ActionLink(text, action, controller);
14         }
15     }

 

2、前台cshtml使用

1 <div class="collapse navbar-collapse  pull-right">
2                 <ul class="nav navbar-nav">
3                     <li>@Html.MenuLink("育兒寶典", "YuErBaoDian", "Home")</li>
4                     <li>@Html.MenuLink("關於我們", "About", "Home")</li>
5                     <li>@Html.MenuLink("聯系我們", "Contact", "Home")</li>
6                 </ul>
7 </div>

 

 

作者:Tommy
出處:https://www.cnblogs.com/XL-Tommy/
本文版權歸作者和博客園所有,歡迎轉載,轉載請標明出處。 
如果您覺得本篇博文對您有所收獲,覺得還算用心,請點擊右下角的 [推薦],謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM