Html.BeginRouteForm 不会为命名路由生成 URL
我正在尝试使用 Html.BeginRouteForm 在 ASP.NET MVC 应用程序中生成表单的操作。我的目标路线是:
//CREATE
routes.MapRoute("map-config-post", "projects/{projectId}/mapconfigs",
new { controller = controllerName, action = "Create" },
new { httpMethod = new RestfulHttpMethodConstraint("POST") });
单元测试并遵循应用程序中的路线是成功的。现在,我想用这个路由创建一个 HTML 表单,它的 URL 是这样的:
@using (Html.BeginRouteForm("map-config-post", new { projectId = Model.Project.Id }))
{
//form stuff
}
这会产生一个带有空白操作属性的 HTML 表单。谷歌搜索了一下,这似乎应该有效。这是一个“新”视图,因此当前(即回发)路由是
projects/1/mapconfigs/new
我希望它发布到
projects/1/mapconfigs
表单操作应该是什么。
如果我不使用助手,我可以手动使这一切发生,但随后我会失去客户端的自动验证功能。
那么,我对我做错了什么有什么想法吗?希望我想做什么很清楚。
I am attempting to use Html.BeginRouteForm to generate the action for a form in my ASP.NET MVC app. The route I am targeting is:
//CREATE
routes.MapRoute("map-config-post", "projects/{projectId}/mapconfigs",
new { controller = controllerName, action = "Create" },
new { httpMethod = new RestfulHttpMethodConstraint("POST") });
Unit testing and following the route in the app is successful. Now, I would like to create an HTML form with this route is it's URL:
@using (Html.BeginRouteForm("map-config-post", new { projectId = Model.Project.Id }))
{
//form stuff
}
This results in an HTML form with a blank action attribute. Googling around, it seems like this is supposed to work. This is a "New" view, so the current (i.e., postback) route is
projects/1/mapconfigs/new
And I want it to post to
projects/1/mapconfigs
which is what the form action should be.
I can manually make this all happen if I don't use the helpers, but then I lose the nice auto-validation stuff on the client-side.
So, any ideas of what I am doing wrong? Hopefully it's clear what I am trying to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎来自您的自定义
RestfulHttpMethodConstraint
约束。使用默认的工作得很好:然后:
生成:
It seems that the problem comes from your custom
RestfulHttpMethodConstraint
constraint. Using the default one works perfectly fine:and then:
generates: