views:

637

answers:

1

I have noticed what Html.BeginForm() method encodes supplied routeValues into action attribute of FORM tag. This works well with POST method. But if method is GET all parameters in action URL are stripped by browser (tested on IE8 and Firefox 3.0.7).

For example, this code in view

<%
    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 URL became /TestController/TestAction not /TestController/TestAction?test=123 (parameter is lost).

Now I use group of Html.Hidden() calls instead of routeValues parameter but I am interested is there another workaround? Should it be considered as a bug in MVC which will be fixed sometime?

+1  A: 

As you can see, the generated HTML is "correct", and has the semantic you want, so this is not a server-side issue, but a client-side one. The browser in this case is stripping away the query part from the action URL, while you expected it to append to the query instead. If you read the specification, the action is not supposed to contain a query (it is an URI, not an URL), so in fact you hit a "limitation" of the HTTP specification.

You are directed to the bare url with no query, because in the HTML you have there is nothing to submit. Try giving a name and a value to the submit field, or add a hidden field, you'll see that the parameters get transmitted in the query.

You should definitely use hidden fields in this case.

Palantir
Interesting why ASP.NET MVC HTML helper BeginForm() doesn't render hidden fields itself when GET method is chosen?
Alexander Prokofyev