ASP.NET MVC2 UpdateModel 不更新白名单中包含的公共属性
我有一个 Foo 类,其字段 UpdateMe 的类型为“Confirmation”,如下所述。
public class Foo
{
public Confirmation UpdateMe{get;set;}
public int BarInt{get;set}
}
public enum Confirmation
{
N = 0,
Y = 1
}
我有一个包含 UpdateMe 的白名单,并按以下方式运行...
[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
public ActionResult Update(Foo foo)
{
if(ModelState.IsValid)
{
//this is the Foo as it exists in the backend..using Linq2Sql read/record behavior
Foo existingFoo = _Service.GetFoo();
string[] whitelist = { "UpdateMe" };
UpdateModel(existingFoo, whitelist);
//do persistence stuff down here...
}
}
模型绑定得很好,传入的 Foo 具有我设置的任何 UpdateMe 值,但是 UpdateModel 过程不会更新该属性。
这已经被极其简单地简化了,但请放心,UpdateModel 正在为通过该操作而来的其他属性工作。
知道为什么这个特定的公共财产没有更新吗?
I have a class Foo with a field UpdateMe of type Confirmation as described below..
public class Foo
{
public Confirmation UpdateMe{get;set;}
public int BarInt{get;set}
}
public enum Confirmation
{
N = 0,
Y = 1
}
I have a whitelist that has UpdateMe, and runs the following way...
[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
public ActionResult Update(Foo foo)
{
if(ModelState.IsValid)
{
//this is the Foo as it exists in the backend..using Linq2Sql read/record behavior
Foo existingFoo = _Service.GetFoo();
string[] whitelist = { "UpdateMe" };
UpdateModel(existingFoo, whitelist);
//do persistence stuff down here...
}
}
the model is bound just fine, the incoming Foo has whatever UpdateMe value I set, however the UpdateModel procedure is not updating the property.
This has been ridiculously simplified, but rest assured the UpdateModel is working for other properties coming through the action.
Any idea why this particular public property is not updating?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是独家新闻。
问题在于该字段被映射到复选框。当不使用 HtmlHelper 编写复选框时,它不会传播到 ModelState 中,因此不会包含在 UpdateModel 中。
当我切换到使用 HtmlHelper 时,无论是否选择(所需),ModelState 都会包含复选框值...但是,这又带来了将枚举类型映射到复选框的丑陋。
Ok, heres the scoop.
The issue is that the field was mapped to a checkbox. When not writing the checkbox using an HtmlHelper it was not propagating into the ModelState, and therefore not being included in the UpdateModel.
When I switched to using an HtmlHelper, the ModelState was then including the checkbox value regardless of being selected(desired)...however this brought back the ugliness of mapping an enum type to a checkbox.