Asp.net mvc ASP.NET MVC4菜单链接在控制器和操作匹配时激活
我有一个ASP.NET MVC4应用程序,左侧导航菜单位于CommonController下,因此从my _Layout.cshtml中,我将其呈现为:Asp.net mvc ASP.NET MVC4菜单链接在控制器和操作匹配时激活,asp.net-mvc,asp.net-mvc-4,menu,Asp.net Mvc,Asp.net Mvc 4,Menu,我有一个ASP.NET MVC4应用程序,左侧导航菜单位于CommonController下,因此从my _Layout.cshtml中,我将其呈现为: <div class="leftmenu"> @Html.Action("LeftMenu", "Common") </div><!--leftmenu--> Im的问题是currentController是“通用”的,因为它位于LeftMenu部分视图的位置 我需要获得主页当前控制
<div class="leftmenu">
@Html.Action("LeftMenu", "Common")
</div><!--leftmenu-->
Im的问题是currentController是“通用”的,因为它位于LeftMenu部分视图的位置
我需要获得主页当前控制器和当前操作,以使其工作
关于如何解决这个问题有什么线索或建议吗
非常感谢我建议将“leftMenu.cshtml”移动到_Layout.cshtml旁边的共享视图文件夹中,并在菜单中使用它,如下所示:
<div class="leftmenu">
@Html.Partial("LeftMenu")
</div>
<!--leftmenu-->
@Html.Partial(“左菜单”)
这样,您将在自定义帮助器中获得正确的操作名称
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);
}
<div class="leftmenu">
@Html.Partial("LeftMenu")
</div>
<!--leftmenu-->