Html.BeginForm loses routeValues ​​with FormMethod.GET

I noticed that the Html.BeginForm () method encodes the provided routeValues ​​parameters into the action attribute of the FORM tag. This works well with the POST method. But if the method is GET, all parameters in the action URL are deleted by the browser (tested in IE8 and Firefox 3.0.7).

For example, this code in sight

<% using (Html.BeginForm("TestAction", "TestController", new { test = 123 }, FormMethod.Get)) { Response.Write("<input type='submit'>"); }; %> 

gives such HTML

 <form action="/TestController/TestAction?test=123" method="get"> <input type='submit'> </form> 

But after submitting the form the URL became / TestController / TestAction not / TestController / TestAction? test = 123 (parameter lost).

Now I use the Html.Hidden () call group instead of the routeValues ​​parameter, but I wonder if there is another workaround? Should it be considered a bug in MVC that will ever be fixed?

+7
parameters asp.net-mvc get-method
source share
1 answer

As you can see, the generated HTML is “correct” and has the semantics you want, so this is not a server-side problem, but a client one. In this case, the browser removes part of the request from the action URL, while you expected it to add the request instead. If you read the specification, the action should not contain a request (this is a URI, not a URL), so in fact you will click the “restriction” of the HTTP specification.

You are directed to a bare URL without a request, because in the HTML that you have, there is nothing to submit. Try to specify a name and value in the send field or add a hidden field, you will see that the parameters are transmitted in the request.

In this case, you must use hidden fields.

+7
source share

All Articles