Html.BeginRouteForm 不会为命名路由生成 URL

发布于 2024-10-14 14:37:41 字数 792 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-10-21 14:37:41

问题似乎来自您的自定义 RestfulHttpMethodConstraint 约束。使用默认的工作得很好

routes.MapRoute(
    "map-config-post",
    "projects/{projectId}/mapconfigs",
    new { controller = "Home", action = "Create" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);

:然后:

@using (Html.BeginRouteForm("map-config-post", new { projectId = Model.Project.Id }))
{ 
}

生成:

<form action="/projects/123/mapconfigs" method="post">

</form>

It seems that the problem comes from your custom RestfulHttpMethodConstraint constraint. Using the default one works perfectly fine:

routes.MapRoute(
    "map-config-post",
    "projects/{projectId}/mapconfigs",
    new { controller = "Home", action = "Create" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);

and then:

@using (Html.BeginRouteForm("map-config-post", new { projectId = Model.Project.Id }))
{ 
}

generates:

<form action="/projects/123/mapconfigs" method="post">

</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文