ASP.NET-MVC2:为什么 TryUpdateModel 忽略对象模型树第二层之后的属性?
也许我在这里遗漏了一些东西,但似乎在使用 TryUpdateModel 时,对象模型树中 3 级或更多级别的任何内容都会被忽略。
例如(简化):
public virtual ActionResult SomeAction(int id, FormCollection form)
{
IValueProvider vpFrom = form.ToValueProvider();
/*
At this stage, vpForm contains:
1)PropertyA
2) PropertyB.SubPropertyA
3) PropertyB.SubPropertyB.SubSubPropertyA
*/
TryUpdateModel(someObjectModel, null, null, null, vpFrom);
//The first two properties are applied, number (3) seems to be ignored
我在这里遗漏了什么吗?如果事情就是这样,有人想出解决方法吗?
Perhaps I'm missing something here, but it seems that anything in the object model tree 3 or more levels down, is ignored when using TryUpdateModel.
For example (simplified):
public virtual ActionResult SomeAction(int id, FormCollection form)
{
IValueProvider vpFrom = form.ToValueProvider();
/*
At this stage, vpForm contains:
1)PropertyA
2) PropertyB.SubPropertyA
3) PropertyB.SubPropertyB.SubSubPropertyA
*/
TryUpdateModel(someObjectModel, null, null, null, vpFrom);
//The first two properties are applied, number (3) seems to be ignored
Am I missing something here? If this is just the way it is, has anyone come up with a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下模型创建的快速项目。
这是我的编辑 - 与您的编辑基本相同
这一切都与正确创建的所有模型完全符合预期。我看到发生的唯一问题是您可能没有从表单传回相同的属性名称。 (例如,不使用
<%: Html.TextBoxFor(model => model.ABCCName)%>
)模型需要无参数构造函数。但我确信你会得到一个关于此的错误 - 除非你正在消耗这个错误。
因此,如果没有有关您的项目的更多信息,将很难提供帮助,因为基本设置会产生预期的结果。
A quick project created with the following model.
Here's my edit - which is essentially the same as yours
This all works exactly as expected with all the models created properly. The only problem that I can see happening is that you possibly aren't passing the same property names back from the form. (by not using
<%: Html.TextBoxFor(model => model.A.B.C.CName)%>
for example)The models require parameterless constructors. But I'm sure you would have gotten an error about that - unless you're consuming the error.
So without more information about your project it will be hard to help as a basic setup produces expected results.
我相信问题出在您的模型类之一。请检查 PropertyB.SubPropertyB.SubSubPropertyA 是否确实是属性而不是字段。属性应该具有 get 和 set 访问器。
I believe the problem is in one of your model classes. Check, please, if PropertyB.SubPropertyB.SubSubPropertyA is really a property but not a field. A property should have get and set accessors.
这是我的清单:
Here's my checklist: