ASP.NET MVC - 带有来自 URL 的附加参数的 POST 操作方法
使用 ASP.net MVC 是否可以将表单 POST 到控制器操作,其中包含的参数不是表单中的参数,而是来自 URL 的参数?
例如
GroupController 中的 Action 方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int idOne, int idTwo, Model model)
{ ... }
路由:
"{controller}/{action}/{idOne}/{idTwo}"
Posted URL:
/Employee/Show/1/42
在此示例中,表单被发布到不同的控制器,模型具有正确的值,但是其他参数具有默认值 0。
我期望的行为是ModelBinder 会看到我有两个与给定路由匹配的参数,并以与 GET 操作相同的方式将当前值 1 和 42 分配给参数。
是否不支持这种行为,或者我错过了什么?
编辑: 需要明确的是,控制器 Employee
的 Show
视图上的表单包含一个发布到不同控制器的表单。我们可以将其称为Group
。
表单操作 URL 如下
/Groups/Create/0/0
所示 表单声明如下
Html.BeginForm("Create", "Groups")
在尝试了 Html.BeginForm
的许多不同重载之后,我发现仅当表单操作 URL 与浏览器中的当前 URL 匹配时才会映射参数地址栏。
因此,如果我导航到 URL /Groups/Create/1/42
我将得到一个新表单。如果我随后提交表单,URL 路由值将传递到 POST 操作。
With ASP.net MVC is it possible to POST a form to a controller action which includes parameters not in the form, but from the URL?
For example
The Action method in GroupController:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int idOne, int idTwo, Model model)
{ ... }
The route:
"{controller}/{action}/{idOne}/{idTwo}"
Posted URL:
/Employee/Show/1/42
In this example, the form is being posted to a different controller, the model has the correct value, however the other parameters have default values 0.
The behavior I was expecting is that the ModelBinder would see that I have two parameters that match the given route, and assign the current values of 1 and 42 to the parameters in the same same way a GET operation works.
Is this behavior not supported, or am I missing something?
EDIT:
To be clear, the form on the Show
view for the controller Employee
contains a form which is posting to a different controller. We can call it Group
.
The form action URL looks like this
/Groups/Create/0/0
The form is declared as follows
Html.BeginForm("Create", "Groups")
After trying many different overloads for Html.BeginForm
I have found that the parameters are only mapped when the form action URL matches the current URL in the browser address bar.
So if i navigate to the URL /Groups/Create/1/42
I will have a new form. If I then submit the form, the URL route values are passed to the POST action.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我正确理解您的问题,您希望呈现的
如果我的问题全部错误,请告诉我:)
If I understand your question correctly, you want the
action
of the rendered<form>
element pointing to URL containing route values. This should be possible with one of the overloads of theHtmlHelper.BeginForm()
extension method:Let me know if I got your question all wrong :)
我很确定您只能从表单内的输入中发布表单数据。您是否考虑过以从 URL 创建表单输入值的方式呈现视图(也许使用 HTML 帮助程序?)。
更新:如果您根本不想使用表单,请使用
ControllerContext.RouteData.Values["idOne"]
,而不是通过方法签名传递它。I'm pretty sure that you can only post form data from inputs inside the form. Have you considered rendering the view in such a way to create form input values off of the URL (perhaps with an HTML helper?).
UPDATE: If you don't want to use the form at all, use
ControllerContext.RouteData.Values["idOne"]
as opposed to passing it in through the method signature.我也遇到了类似的问题,配置RouteConfig已经解决了这个问题。
...和...
Html .BeginRouteForm()
以及所有作品
I had a similar problem, the configuration RouteConfig has solved this problem.
...And...
Html.BeginRouteForm()
And all works
我最近也遇到了这个问题,因为我有不同的路线,所以它映射到默认路线,而不考虑我传入的额外路线参数。
因此,为了让它快速工作,我使用 form 编写了表单标签,并使用 @Url.Action 来创建所需的操作。
I recently had this issue too, and because I had a different route, it was mapping to the default route, without taking into account the extra route params that I was passing in.
So to get it working quickly, I wrote the form using form tags instead, and used @Url.Action to create the required action.
进入“创建”视图后,用于到达那里的路线值需要重新发布到后期操作。
因此,一个选择是使用一对隐藏层来保存来自路线的 id。这样,一旦您发布格式,其值就会与其他输入一起发布。
Once you get to your Create view, your route values used to get there need to be re-posted to the post action.
So, one option is having a pair of hiddens, to hold the ids that came from the route. This way, once you post the formas, its values are posted along with the other inputs.