为什么这个在 .NET 应用程序中使用 @Html.ActionLink 的导航栏菜单没有像我预期的那样工作?
Why this navbar menu using @Html.ActionLink in a .NET application is not working as I am expecting?
我不太喜欢 .NET,我遇到了以下问题。在一个页面中,我有一个 BootStrap 导航栏菜单,如下所示:
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
@Html.ActionLink("Vidly_v2", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor01">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">@Html.ActionLink("Home", "Index", "Home", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Customers", "Index", "Customers", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Movies", "Index", "Movies", new { @class = "nav-link" })</li>
</ul>
@Html.Partial("_LoginPartial")
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" placeholder="Search" type="text">
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
我对这 3 个链接有以下问题:
<li class="nav-item active">@Html.ActionLink("Home", "Index", "Home", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Customers", "Index", "Customers", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Movies", "Index", "Movies", new { @class = "nav-link" })</li>
因为点击所有这些链接,用户被重定向到以下 URL(与之前的 标签的顺序相同:
http://localhost:60048/Movies?Length=4
和
http://localhost:60048/Movies?Length=9
和
http://localhost:60048/Movies?Length=6
为什么?怎么了?我错过了什么?我该如何解决这个问题?
您使用的辅助方法不正确!
您当前正在使用 ActionLink
助手的以下重载。
public static MvcHtmlString ActionLink (this HtmlHelper helper,
string linkText,
string actionName,
string controllerName,
object routeValues);
最后一个参数用于传递将用于构建查询字符串的路由值。当前,您正在将 html 属性的匿名对象传递给它。
使用这个重载
public static MvcHtmlString ActionLink (this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes);
如果您没有要传递的任何路由值,只需为第 4 个参数(路由值)
传递 null
@Html.ActionLink("Movies", "Index", "Movies", null, new { @class = "nav-link" })
根据 ActionLink
helper overload list,您正在使用 4 个重载:
public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper,
string linkText, string actionName, object routeValues, object htmlAttributes);
在这种情况下,控制器名称被视为 routeValues
参数,这成为 Length
参数值在每个 href
link 上变化的原因("Home".Length
= 4, "Customers".Length
= 9 & "Movies".Length
= 6).
您应该使用具有 5 个重载的 ActionLink
助手:
public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper,
string linkText, string actionName, string controllerName, object routeValues,
object htmlAttributes);
通过将 null
值传递给 routeValues
参数,如下所示:
<li class="nav-item active">@Html.ActionLink("Home", "Index", "Home", null, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Customers", "Index", "Customers", null, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Movies", "Index", "Movies", null, new { @class = "nav-link" })</li>
我不太喜欢 .NET,我遇到了以下问题。在一个页面中,我有一个 BootStrap 导航栏菜单,如下所示:
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
@Html.ActionLink("Vidly_v2", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor01">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">@Html.ActionLink("Home", "Index", "Home", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Customers", "Index", "Customers", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Movies", "Index", "Movies", new { @class = "nav-link" })</li>
</ul>
@Html.Partial("_LoginPartial")
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" placeholder="Search" type="text">
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
我对这 3 个链接有以下问题:
<li class="nav-item active">@Html.ActionLink("Home", "Index", "Home", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Customers", "Index", "Customers", new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Movies", "Index", "Movies", new { @class = "nav-link" })</li>
因为点击所有这些链接,用户被重定向到以下 URL(与之前的 标签的顺序相同:
http://localhost:60048/Movies?Length=4
和
http://localhost:60048/Movies?Length=9
和
http://localhost:60048/Movies?Length=6
为什么?怎么了?我错过了什么?我该如何解决这个问题?
您使用的辅助方法不正确!
您当前正在使用 ActionLink
助手的以下重载。
public static MvcHtmlString ActionLink (this HtmlHelper helper,
string linkText,
string actionName,
string controllerName,
object routeValues);
最后一个参数用于传递将用于构建查询字符串的路由值。当前,您正在将 html 属性的匿名对象传递给它。
使用这个重载
public static MvcHtmlString ActionLink (this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes);
如果您没有要传递的任何路由值,只需为第 4 个参数(路由值)
传递null
@Html.ActionLink("Movies", "Index", "Movies", null, new { @class = "nav-link" })
根据 ActionLink
helper overload list,您正在使用 4 个重载:
public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper,
string linkText, string actionName, object routeValues, object htmlAttributes);
在这种情况下,控制器名称被视为 routeValues
参数,这成为 Length
参数值在每个 href
link 上变化的原因("Home".Length
= 4, "Customers".Length
= 9 & "Movies".Length
= 6).
您应该使用具有 5 个重载的 ActionLink
助手:
public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper,
string linkText, string actionName, string controllerName, object routeValues,
object htmlAttributes);
通过将 null
值传递给 routeValues
参数,如下所示:
<li class="nav-item active">@Html.ActionLink("Home", "Index", "Home", null, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Customers", "Index", "Customers", null, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Movies", "Index", "Movies", null, new { @class = "nav-link" })</li>