ASP.NET-MVC2: Why does TryUpdateModel ignore properties after the second level of the object model tree?

Maybe something is missing for me, but it seems that anything in the object model tree of 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? If so, does anyone come up with a workaround?

+3
source share
3 answers

A quick project created using 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 is my edit, which essentially matches 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);
}

, , , . , , , , , . (, <%: Html.TextBoxFor(model => model.A.B.C.CName)%>)

. , - .

, , .

+5

, . , , PropertyB.SubPropertyB.SubSubPropertyA , . .

+3

:

+1
source

Source: https://habr.com/ru/post/1758156/


All Articles