Binding model to POST with query string and form parameters

What is the specified behavior for form binding in ASP.NET/MVC if you have a POST form and the action has request parameters and , do you have form data?

For example:

<form action="my/action?foo=1" method="post"> <input type="hidden" name="bar" value="2"> </form> 

If such a form appears, if the controller receives both foo and bar or only one of them?

+7
source share
3 answers

The controller will receive both values. By default, the connecting device will try to find matches for the parameters from both the URI (query string or route parameters) and the body (and form data is supported out of the box).

+7
source

Note that you can see that this is supported by the Html.BeginForm helper, you do this through routeValues:

 @Html.BeginForm("ActionName", "ControllerName", new { foo = "1" }) 

It essentially generates the same html as the form tag, but wants to post to those who find this question, and wants to know how to pass additional values ​​that are not part of the form using the BeginForm helper.

+1
source

I think he should be able to get both. In this case, I would create a ViewModel that contains two string or int properties, one with the name "foo" and the other with the name "bar" and your ActionResult accepts the ViewModel. You should see both values.

0
source

All Articles