在ASP.NET MVC可能的错误与表单值被替换 [英] Possible bug in ASP.NET MVC with form values being replaced

查看:147
本文介绍了在ASP.NET MVC可能的错误与表单值被替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有在与ASP.NET MVC一个问题,如果我有在每一个使​​用相同名称的页面上有多个形式,但不同类型(收音机/隐藏/等),然后当第一种形式员额(我选择例如日期单选按钮),如果表单重新渲染(比如作为结果页的一部分),我似乎有问题的检索类别的潜在价值上其他形式被改变为最后的单选按钮的值(在这种情况下,SearchType.Name)

I appear to be having a problem with ASP.NET MVC in that, if I have more than one form on a page which uses the same name in each one, but as different types (radio/hidden/etc), then, when the first form posts (I choose the 'Date' radio button for instance), if the form is re-rendered (say as part of the results page), I seem to have the issue that the hidden value of the SearchType on the other forms is changed to the last radio button value (in this case, SearchType.Name).

下面是减少目的的例子形式。

Below is an example form for reduction purposes.

<% Html.BeginForm("Search", "Search", FormMethod.Post); %>
  <%= Html.RadioButton("SearchType", SearchType.Date, true) %>
  <%= Html.RadioButton("SearchType", SearchType.Name) %>
  <input type="submit" name="submitForm" value="Submit" />
<% Html.EndForm(); %>

<% Html.BeginForm("Search", "Search", FormMethod.Post); %>
  <%= Html.Hidden("SearchType", SearchType.Colour) %>
  <input type="submit" name="submitForm" value="Submit" />
<% Html.EndForm(); %>

<% Html.BeginForm("Search", "Search", FormMethod.Post); %>
  <%= Html.Hidden("SearchType", SearchType.Reference) %>
  <input type="submit" name="submitForm" value="Submit" />
<% Html.EndForm(); %>

结果页面源代码(这将是在结果页的一部分)

Resulting page source (this would be part of the results page)

<form action="/Search/Search" method="post">
  <input type="radio" name="SearchType" value="Date" />
  <input type="radio" name="SearchType" value="Name" />
  <input type="submit" name="submitForm" value="Submit" />
</form>

<form action="/Search/Search" method="post">
  <input type="hidden" name="SearchType" value="Name" /> <!-- Should be Colour -->
  <input type="submit" name="submitForm" value="Submit" />
</form>

<form action="/Search/Search" method="post">
  <input type="hidden" name="SearchType" value="Name" /> <!-- Should be Reference -->
  <input type="submit" name="submitForm" value="Submit" />
</form>

请任何人都可以用其他RC1证实?

Please can anyone else with RC1 confirm this?

也许是因为我使用的枚举。我不知道。我要补充一点,我可以通过使用手动输入()代码隐藏字段绕过这个问题,但如果我使用MVC的标签(小于(%)= Html.Hidden(...)%>),.NET MVC取代他们每一次。

Maybe it's because I'm using an enum. I don't know. I should add that I can circumvent this issue by using 'manual' input () tags for the hidden fields, but if I use MVC tags (<%= Html.Hidden(...) %>), .NET MVC replaces them every time.

非常感谢。

更新:

我今天再次看到了这个错误。看来,这当您返回一个发布页面,并使用MVC一套隐藏的表单标签与HTML辅助作物发挥得淋漓尽致。我已经联系菲尔哈克的这个,因为我不知道还有什么地方转了,我不认为这应该是预期的行为由大卫指定。

I've seen this bug again today. It seems that this crops its head when you return a posted page and use MVC set hidden form tags with the Html helper. I've contacted Phil Haack about this, because I don't know where else to turn, and I don't believe that this should be expected behaviour as specified by David.

推荐答案

是的,这种行为目前是设计使然。即使你明确地设定值,如果你回发到相同的URL,我们期待在模型状态和使用的价值在那里。一般情况下,这使我们能够显示您所提交的回传,而不是原始值的值。

Yes, this behavior is currently by design. Even though you're explicitly setting values, if you post back to the same URL, we look in model state and use the value there. In general, this allows us to display the value you submitted on postback, rather than the original value.

有两种可能的解决方案:

There are two possible solutions:

使用每个字段的唯一的名称。请注意,在默认情况下我们使用的名称您指定为HTML元素的ID。这是无效的HTML有多个元素具有相同的ID。因此,使用唯一的名称是很好的做法。

Use unique names for each of the fields. Note that by default we use the name you specify as the id of the HTML element. It's invalid HTML to have multiple elements have the same id. So using unique names is good practice.

不要使用隐藏的帮手。好像你真的不需要它。相反,你可以这样做:

Do not use the Hidden helper. It seems like you really don't need it. Instead, you could do this:

<input type="hidden" name="the-name" 
  value="<%= Html.AttributeEncode(Model.Value) %>" />

当然,因为我想了解更多这方面,不断变化的基础上回发的价值是有道理的文本框,但意义不大隐藏的投入。我们不能改变V1.0,但我会考虑它V2。但是,我们需要想通过这样的改变精心的影响。

Of course, as I think about this more, changing the value based on a postback makes sense for Textboxes, but makes less sense for hidden inputs. We can't change this for v1.0, but I'll consider it for v2. But we need to think through carefully the implications of such a change.

这篇关于在ASP.NET MVC可能的错误与表单值被替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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