Html.Hidden建立在MVC 2应用程序错误的值数据 [英] Html.Hidden builds wrong value data in MVC 2 app

查看:165
本文介绍了Html.Hidden建立在MVC 2应用程序错误的值数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个ID值,我通过在一个隐藏字段。当用户提交表单,我需要隐藏字段我的更新。更新后,一个新的值被放置在模型中的隐藏字段和发送回图。什么似乎太奇怪了是助手总是使用第一个值,永远不会更新。例如,看一下从查看以下内容:

I am using an id value that I pass in a hidden field. When the user submits the form I need the hidden field for my update. After the update, a new value is placed in the hidden field in the model and sent back to the view. What seems so strange is the helper always uses the first value, never updates. For example, look at the following from the View:

<%: Html.Hidden("MyId",Model.MyId)  %>
<%: Model.MyId %>

在浏览器产量看看源第一次:

First time in a look at the source in the browser yields:

<input type="hidden" id="MyId" name="MyId" value="1" />
1

**提交回控制器和模型更新MYID属性为2。

** submit back to controller and model updates the MyId property to 2.

回到浏览器我现在发现:

Back at the browser I now find:

<input type="hidden" id="MyId" name="MyId" value="1" />
2

非常相同模型属性有不同的值!助手方法以某种方式从以前的模型实例或东西抓住它?

The very same model property has different values! The helper method is somehow grabbing it from a prior model instance or something?

任何帮助非常AP preciated什么我不理解。 BTW..get具有相同的行为 Html.TextBox Html.TextBoxFor

Any help greatly appreciated on what I am not understanding. BTW..get the same behavior with Html.TextBox and Html.TextBoxFor.

感谢。

推荐答案

这是HTML佣工是如何工作的,它是由设计。当绑定他们会先看看在GET / POST请求的值,看看是否值是present,之后在模型中。如果在请求中发现一个值,他们将根本就不理你在模型中设置的值。

That's how HTML helpers work and it's by design. When binding they will first look at the value in the GET/POST request to see if the value is present and after that in the model. If a value is found in the request they will simply ignore the value you set in the model.

通常你不应该修改你的控制器动作内请求发送的数据。但是如果你无论如何决定做它,你将需要或者推出自己的助手或者干脆:

Normally you are not supposed to modify the data sent in the request inside your controller action. But if anyhow you decide to do it you will need to either roll your own helper or simply:

<input type="hidden" name="MyId" value="<%= Model.MyId %>" />

这篇关于Html.Hidden建立在MVC 2应用程序错误的值数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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