HTMLHelper。 ActionLink 总是设置当前控制器而不是传递另一个控制器
HtmlHelper. ActionLink always set the current controller instead passing another controller
我正在使用 HtmlHelper class,其中我使用了一些代码并将其设置为我的布局导航栏作为菜单。
下面是相同的代码:
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(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
{
return new MvcHtmlString("<li class=\"nav-item active\">" + helper.ActionLink(text, action, controller, new { @class="nav-link" }) + "</li>");
}
return new MvcHtmlString("<li class=\"nav-item\">" + helper.ActionLink(text, action, controller, new { @class = "nav-link" }) + "</li>");
}
然后我在布局页面中将菜单设置如下:
@Html.MenuLink("Search", "Search", "Home")
现在,当我 运行 应用程序时,它首先加载登录页面,其中登录路径为:Account/Login
现在在搜索菜单中,当我悬停这个菜单时,我发现这个菜单 url 也设置为 Account/Search?Length=4.
但是我通过了这个菜单的“Home”控制器。
我调试了 html 帮助程序 class 代码,发现它设置了从布局传递的正确操作和控制器。
不知道这个url在哪里改的。
求推荐。
使用以下代码:
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(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
{
return new MvcHtmlString("<li class=\"nav-item active\">" + helper.ActionLink(text, action, controller,null, new { @class="nav-link" }) + "</li>");
}
return new MvcHtmlString("<li class=\"nav-item\">" + helper.ActionLink(text, action, controller,null, new { @class = "nav-link" }) + "</li>");
}
当你使用
helper.ActionLink(text, action, controller,new { @class="nav-link" })
那么它指的是当前控制器,因为你没有指定 routeValue 所以,当你想从一个控制器重定向到另一个控制器时,如果没有 routevalue 重定向方法,那么将 routevalue 指定为 null。
所以,使用 helper.ActionLink(text, action, controller,null, new { @class="nav-link" })
我正在使用 HtmlHelper class,其中我使用了一些代码并将其设置为我的布局导航栏作为菜单。
下面是相同的代码:
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(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
{
return new MvcHtmlString("<li class=\"nav-item active\">" + helper.ActionLink(text, action, controller, new { @class="nav-link" }) + "</li>");
}
return new MvcHtmlString("<li class=\"nav-item\">" + helper.ActionLink(text, action, controller, new { @class = "nav-link" }) + "</li>");
}
然后我在布局页面中将菜单设置如下:
@Html.MenuLink("Search", "Search", "Home")
现在,当我 运行 应用程序时,它首先加载登录页面,其中登录路径为:Account/Login
现在在搜索菜单中,当我悬停这个菜单时,我发现这个菜单 url 也设置为 Account/Search?Length=4.
但是我通过了这个菜单的“Home”控制器。
我调试了 html 帮助程序 class 代码,发现它设置了从布局传递的正确操作和控制器。
不知道这个url在哪里改的。
求推荐。
使用以下代码:
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(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
{
return new MvcHtmlString("<li class=\"nav-item active\">" + helper.ActionLink(text, action, controller,null, new { @class="nav-link" }) + "</li>");
}
return new MvcHtmlString("<li class=\"nav-item\">" + helper.ActionLink(text, action, controller,null, new { @class = "nav-link" }) + "</li>");
}
当你使用
helper.ActionLink(text, action, controller,new { @class="nav-link" })
那么它指的是当前控制器,因为你没有指定 routeValue 所以,当你想从一个控制器重定向到另一个控制器时,如果没有 routevalue 重定向方法,那么将 routevalue 指定为 null。
所以,使用 helper.ActionLink(text, action, controller,null, new { @class="nav-link" })