Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model binding on POST with query string AND form parameters

What is the defined behavior for form binding in ASP.NET/MVC if you POST a form and its action has query parameters and 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 is submitted should the controller get both foo and bar or only one of them?

like image 860
JoelFan Avatar asked May 04 '12 19:05

JoelFan


Video Answer


2 Answers

The controller will get both values. The default model binder will try to find matches for the parameters from both the URI (either query string or route parameters) or the body (and forms data is supported out-of-the-box).

like image 121
carlosfigueira Avatar answered Oct 23 '22 08:10

carlosfigueira


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

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

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

like image 33
AaronLS Avatar answered Oct 23 '22 08:10

AaronLS