ASP.NET-MVC2:为什么 TryUpdateModel 忽略对象模型树第二层之后的属性?

发布于 2024-09-13 03:29:32 字数 596 浏览 9 评论 0原文

也许我在这里遗漏了一些东西,但似乎在使用 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 技术交流群。

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

发布评论

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

评论(3

゛清羽墨安 2024-09-20 03:29:32

使用以下模型创建的快速项目。

public class TestModel {
    public TestModelA A { get; set; }
    public string Name { get; set; }
}

public class TestModelA {
    public TestModelB B { get; set; }
    public string Name { get; set; }
}

public class TestModelB {
    public TestModelC C { get; set; }
    public string Name { get; set; }
}

public class TestModelC {
    public TestModelD D { get; set; }
    public string Name { get; set; }
}

public class TestModelD {
    public TestModelE E { get; set; }
    public string Name { get; set; }
}

public class TestModelE {
    public string Name { get; set; }
}

这是我的编辑 - 与您的编辑基本相同

[HttpPost]
public ActionResult Edit(FormCollection form) {
    IValueProvider vpFrom = form.ToValueProvider();

    Models.TestModel t = new Models.TestModel();

    TryUpdateModel(t, null, null, null, vpFrom);

    return View(t);
}

这一切都与正确创建的所有模型完全符合预期。我看到发生的唯一问题是您可能没有从表单传回相同的属性名称。 (例如,不使用 <%: Html.TextBoxFor(model => model.ABCCName)%>

模型需要无参数构造函数。但我确信你会得到一个关于此的错误 - 除非你正在消耗这个错误。

因此,如果没有有关您的项目的更多信息,将很难提供帮助,因为基本设置会产生预期的结果。

A quick project created with the following model.

public class TestModel {
    public TestModelA A { get; set; }
    public string Name { get; set; }
}

public class TestModelA {
    public TestModelB B { get; set; }
    public string Name { get; set; }
}

public class TestModelB {
    public TestModelC C { get; set; }
    public string Name { get; set; }
}

public class TestModelC {
    public TestModelD D { get; set; }
    public string Name { get; set; }
}

public class TestModelD {
    public TestModelE E { get; set; }
    public string Name { get; set; }
}

public class TestModelE {
    public string Name { get; set; }
}

Here's my edit - which is essentially the same as yours

[HttpPost]
public ActionResult Edit(FormCollection form) {
    IValueProvider vpFrom = form.ToValueProvider();

    Models.TestModel t = new Models.TestModel();

    TryUpdateModel(t, null, null, null, vpFrom);

    return View(t);
}

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.

清音悠歌 2024-09-20 03:29:32

我相信问题出在您的模型类之一。请检查 PropertyB.SubPropertyB.SubSubPropertyA 是否确实是属性而不是字段。属性应该具有 getset 访问器。

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.

帅的被狗咬 2024-09-20 03:29:32

这是我的清单:

  1. 确保您在表单请求中取回值。请求[“ABCName”]等。
  2. 所有必填字段都在表单上。
  3. 我在使用 Linq to SQL 时遇到了 deleteOnNull 问题: 如果您使用的是 L2SQL,如何从设计器设置 DeleteOnNull 供将来参考。

Here's my checklist:

  1. Make sure you're getting the value back in the form request. Request["A.B.C.Name"] and etc.
  2. All the required fields are on the form.
  3. I had deleteOnNull issue with Linq to SQL: How to set DeleteOnNull from designer for future ref if you're using L2SQL.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文