为什么 Html.BeginForm 生成空操作?
我在一个名为 Admin
的区域中有一个控制器
public class SiteVisitController : Controller
{
public ViewResult ReadyForCompletion() { ... }
public ViewResult CompleteAndExport() { ... }
}
,还有一个视图 (ReadyForCompletion.cshtml
),该视图已回发到同一类上的不同控制器操作
@using (Html.BeginForm( "CompleteAndExport", "SiteVisit" ))
{
<input type="submit" value="Complete & Export" />
}
为此表单生成的 HTML有一个空白操作:
<form action="" method="post"> <input type="submit" value="Complete & Export" />
</form>
我想知道为什么它有一个空白操作?有关更多信息,我还添加了一个
@Url.RouteUrl(new { controller = "ReadyForCompletion", action = "SiteVisit", area = "Admin" })
也打印出空字符串的内容。另外,如果我使用空的 Html.BeginForm()
它会生成正确的操作。
注册路线有
context.MapRoute(
"Admin_manyParams",
"Admin/{controller}/{action}/{id}/{actionId}",
new { action = "Index", id = UrlParameter.Optional, actionId = UrlParameter.Optional }
);
I have a controller in an area called Admin
public class SiteVisitController : Controller
{
public ViewResult ReadyForCompletion() { ... }
public ViewResult CompleteAndExport() { ... }
}
and a view (ReadyForCompletion.cshtml
) that has posts back to a different controller action on the same class
@using (Html.BeginForm( "CompleteAndExport", "SiteVisit" ))
{
<input type="submit" value="Complete & Export" />
}
The generated HTML for this form has a blank action:
<form action="" method="post"> <input type="submit" value="Complete & Export" />
</form>
I want to know why this has a blank action? For more info, I also added in a
@Url.RouteUrl(new { controller = "ReadyForCompletion", action = "SiteVisit", area = "Admin" })
which also printed out an empty string. Also, if I use an empty Html.BeginForm()
it generates the correct action.
Registered routes are
context.MapRoute(
"Admin_manyParams",
"Admin/{controller}/{action}/{id}/{actionId}",
new { action = "Index", id = UrlParameter.Optional, actionId = UrlParameter.Optional }
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您的问题是由连续的可选参数引起的。在我更改路线以包含两个可选参数之前,我无法复制您的问题。
请参阅:这个解释问题的文章
I believe your problem is caused by having consecutive optional parameters. I was not able to replicate your problem until I changed the route to contain two optional parameters.
See: This article which explains the problem
对于那些使用 ASP.NET Core 遇到此问题的人来说,根本原因是相同的,但解决方案略有不同。我第一次在 Core 中看到这个,在调用
.MapRoutes()
时使用多个默认值。例如,解决方法是将默认值放入字符串模板中:
YMMV。
For those of you encountering this issue using ASP.NET Core the root cause is the same, though the solution is slightly different. I first saw this in Core using multiple default values when calling
.MapRoutes()
. E.g.The workaround is to place the default values into the string template:
YMMV.