Html.BeginForm失去routeValues​​与FormMethod.GET [英] Html.BeginForm loses routeValues with FormMethod.GET

查看:208
本文介绍了Html.BeginForm失去routeValues​​与FormMethod.GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到了什么的 Html.BeginForm()的方法连接codeS提供的 routeValues​​ 的成动作 FORM标签的属性。这与POST方法效果很好。但是,如果方法是GET在操作URL的所有参数是通过浏览器(IE8上和Firefox 3.0.7测试)剥离。

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).

例如,这code鉴于

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

给出了这样的HTML

gives such HTML

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

但后提交表单的网址成了 /的TestController / TestAction 不是 /的TestController / TestAction?测试= 123 (参数丢失)。

现在我使用的 Html.Hidden()的调用,而不是的 routeValues​​ 的参数组,但我感兴趣的是有另一种解决方法吗?它应该被视为MVC中的错误将被固定的某个时候?

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?

推荐答案

正如你所看到的,所生成的HTML是正确的,并有你想要的语义,所以这不是一个服务器端的问题,而是一个客户端机端之一。
在这种情况下,浏览器被剥离了查询部分从操作URL,而你期望它追加到查询,而不是。如果你读了规范,动作不应该包含一个查询(它是一个URI,而不是URL),所以实际上你打一个HTTP规范的限制。

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.

您被定向到没有查询裸URL,因为在HTML你有没有什么提交。尝试给一个名字和一个值,以提交场,或添加隐藏字段,你会看到这些参数得到查询传输。

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.

这篇关于Html.BeginForm失去routeValues​​与FormMethod.GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆