无法更改 ASP.NET MVC 3 中文本框的值

发布于 2024-12-08 23:57:28 字数 1368 浏览 3 评论 0 原文

我在更改 MVC 3 中文本框的值时遇到问题。

我有一个由控制器返回并输入到自定义视图模型的简单视图。视图上有一个文本框,它绑定到视图模型上的字符串属性。如果我在初始加载时填充此字符串,则文本框值将包含该字符串的值。但是,如果我随后将表单发回同一 URL 并更改视图模型字符串值并重新加载视图,则文本框包含发回的值,而不是控制器为视图模型提供的值。

我创建了一个简单的测试项目,以尽可能简单的方式重现问题。

查看型号:
http://www.codetunnel.com/content/images/textboxproblem/ViewModel。 jpg

控制器:
http://www.codetunnel.com/content/images/textboxproblem/Controller。 jpg

查看:
http://www.codetunnel.com/content/images/textboxproblem/View。 jpg

在视图中,我显示视图模型字符串值,并将字符串属性绑定到文本框。

初始加载时一切看起来都很好:
http://www.codetunnel.com/content/images/textboxproblem/InitialLoadTest。 jpg

但是,如果我更改文本框的值,然后按 Enter 键发布表单,事情就不会按我的预期发生。显示的值是控制器在操作方法中设置的值,但文本框保留回发的值:

http://www.codetunnel.com/content/images/textboxproblem/POSTTest.jpg

我不确定问题是什么。

I am having an issue changing the value of a text box in MVC 3.

I have a simple view that is returned by a controller and is typed to a custom view model. There is a text box on the view that is bound to a string property on the view model. If I populate this string on initial load then the text box value contains the value of the string. However, if I then post a form back to the same URL and change the view model string value and reload the view then the text box contains the value that was posted back, not the value that the controller supplied for the view model.

I created a simple test project to recreate the issue in the simplest way possible.

View Model:
http://www.codetunnel.com/content/images/textboxproblem/ViewModel.jpg

Controller:
http://www.codetunnel.com/content/images/textboxproblem/Controller.jpg

View:
http://www.codetunnel.com/content/images/textboxproblem/View.jpg

In the view I display the view model string value and I also bind the string property to a text box.

On initial load everything looks fine:
http://www.codetunnel.com/content/images/textboxproblem/InitialLoadTest.jpg

However, if I change the value of the text box and then press enter to POST the form things do not happen as I expected. The displayed value is the value the controller set in the action method, but the text box retains the value that was posted back:

http://www.codetunnel.com/content/images/textboxproblem/POSTTest.jpg

I'm not sure what the problem is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2024-12-15 23:57:28

(不幸的是)这是正确的行为。使用 TextBoxFor 或其他数据绑定元素时,创建的元素首先与现有的 ModelState 绑定,然后再与模型绑定。

解决方案是清除 [HttpPost] 操作中的 ModelState,以便 POST 操作返回的视图中的元素将与模型绑定。在您的 POST 操作中,添加以下内容:

ModelState.Clear();

就个人而言,我认为设计不正确,但清除 ModelState 会给您带来预期的行为。

That is (unfortunately) the correct behavior. When using TextBoxFor or other data-bound elements, the created element is first bound against the existing ModelState, before it is bound against the model.

The solution is to clear the ModelState in your [HttpPost] action, so that the elements in the View returned by the POST action will be bound against the model. In your POST action, add the following:

ModelState.Clear();

Personally, I think the design is incorrect, but clearing ModelState will give you the behavior you expected.

ゞ花落谁相伴 2024-12-15 23:57:28

这是 MVC 应用程序的预期行为。

当您发布帖子时,它会将值存储在 ModelState 中,并且 Html 帮助程序使用该值而不是 Model 值。

如果需要,您可以编写自己的 html 帮助程序或使用标准 html 来解决此问题。

That is the expected behaviour of an MVC application.

When you make a post it stored the value in the ModelState and the Html helpers use that value instead of the Model value.

You can write you're own html helper or use a standard html <input/> to work around this if needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文