2013-07-02 19 views
0

我有一個ASP.NET應用程序MVC4和我的左側導航菜單是一個CommonController下,所以從我的_Layout.cshtml我使其喜歡:ASP.NET MVC4菜單鏈接激活時,控制器和動作相匹配

<div class="leftmenu">   
    @Html.Action("LeftMenu", "Common") 
</div><!--leftmenu--> 

現在,在我的局部視圖LeftMenu我有顯示菜單項的所有HTML代碼,我想要做的是突出基於當前頁面控制器和操作的右側菜單項。

LeftMenu.cshtml

<ul class="nav nav-tabs nav-stacked"> 
<li class="nav-header">Navigation Menu</li> 
<li>@Html.MenuLink("Test Link", "Index", "Home", "active",true)</li> 
...... 

現在即時通訊使用下面的助手代碼:

public static HtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string activeClass, bool checkAction) 
     { 
      string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action"); 
      string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller"); 

      if (string.Compare(controllerName, currentController, StringComparison.OrdinalIgnoreCase) == 0 && ((!checkAction) || string.Compare(actionName, currentAction, StringComparison.OrdinalIgnoreCase) == 0)) 
      { 
       return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = activeClass }); 
      } 

      return htmlHelper.ActionLink(linkText, actionName, controllerName); 

     } 

具有即時通訊的問題是,currentController是 「共同」,因爲它在那裏LeftMenu部分視圖位於。

我需要獲取主頁當前控制器和當前操作,以使其工作。

任何線索或建議如何解決它?

非常感謝

回答

0

我建議將您的「leftMenu.cshtml」到共享視圖您_Layout.cshtml旁邊的文件夾,然後在你的菜單中使用這樣的:

<div class="leftmenu">   
    @Html.Partial("LeftMenu") 
</div> 
<!--leftmenu--> 

這樣,您將在自定義助手中獲取正確的操作名稱。