我在剃须刀页面上有以下链接:

@Html.ActionLink("Create New Profile", "Create", "Profile", new { @class="toplink" })

它显示在视图源中,如下所示:

<a href="/admin/profile/create?length=7" class="toplink">Create New Profile</a>

当我单击链接时,URL就是这样:

http://localhost:54876/admin/profile/create?length=7

我不要 ?length=7. 。为什么会生成此自动?

有帮助吗?

解决方案

ActionLink 覆盖您正在使用匹配到 (字符串linkText,字符串actionname,对象routevalues,对象htmlattributes) 覆盖。因此,您的“配置文件”值正在传递给 routeValues 范围。此函数相对于此参数的行为是将其上的所有公共属性添加到用于生成链接的路由值列表中。由于字符串只有一个公共属性(长度),因此您最终会以“长度= 7”。

您要使用的正确超载是 (字符串linkText,字符串ActionName,String ControllerName,Object Routevalues,Object HTMLATTRIBUTES) 您称其为loke:

@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"})

其他提示

我不确定其确切原因,但将其更改为:

@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink" })

我不知道当您关闭最后一个参数时,哪个过载MVC正在选择(htmlattributes 是添加的),但这将修复它。这些日子之一我将调查并确切地弄清楚发生了什么。

要注意的另一件事,因为您正在定义控制器 @ActionLink, ,您可能不需要这样做,例如,您的“创建新配置文件”的观点 @ActionLink 在以“/admin/profile/index.cshtml”中表达,该视图列出了现有配置文件,在这种情况下,您无需在此处定义控制器 @ActionLink 作为 @ActionLink 已经相对于 ProfileController, ,所以你 @ActionLink 可能

@Html.ActionLink("Create New Profile", "Create", null, new { @class="toplink" })

我用了 null 代替 new{} 正如明显的答案一样,我认为这是更合适的。 ActionLink超载并不是有史以来最简单的事情。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top