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

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

问题描述

我似乎在使用 ASP.NET MVC 时遇到了问题,如果页面上有多个表单,每个表单都使用相同的名称,但类型不同(无线电/隐藏/等),那么,当第一个表单发布时(例如,我选择日期"单选按钮),如果表单被重新呈现(比如作为结果页面的一部分),我似乎遇到了 SearchType 的隐藏值的问题其他表单更改为最后一个单选按钮值(在本例中为 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 帮助程序时,这似乎会引起注意.我已经就此事联系了 Phil Haack,因为我不知道还能去哪里,而且我不相信这应该是 David 指定的预期行为.

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.多个元素具有相同的 id 是无效的 HTML.因此,使用唯一名称是一种很好的做法.

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天全站免登陆