Need help using DefaultModelBinder for nested model

There are a couple of related questions, but I cannot find an answer that works.

Assuming I have the following models:

public class EditorViewModel
{
  public Account Account {get;set;}
  public string SomeSimpleStuff {get;set;}
}

public class Account
{
  public string AccountName {get;set;}
  public int MorePrimitivesFollow {get;set;}
}

and an extension ViewPage<EditorViewModel>view that does the following:

<%= Html.TextBoxFor(model => model.Account.AccountName)%>
<%= Html.ValidationMessageFor(model => model.Account.AccountName)%>
<%= Html.TextBoxFor(model => model.SomeSimpleStuff )%>
<%= Html.ValidationMessageFor(model => model.SomeSimpleStuff )%>

and my controller looks like this:

[HttpPost]
public virtual ActionResult Edit(EditorViewModel account)
{ /*...*/ }

How can I get DefaultModelBinder to properly bind my EditorViewModel? Without doing anything special, I get an empty instance of my EditorViewModel with all null or the default value.

The closest I came, UpdateModelmanually calling :

[HttpPost]
public virtual ActionResult Edit(EditorViewModel account)
{
    account.Account = new Account();
    UpdateModel(account.Account, "Account");
    // this kills me:
    UpdateModel(account);

, UpdateModel account ( ViewModel), " ... ". , , .

?

+3
1

, , , account.accountname, AccountName Model.

, , , [Bind (Prefix = ")]. " , account, , ". account.accountname account.account.accountname.

- :

foo, , * foo. **. , foo, FirstName, , foo.FirstName = John, .

, a * foo. ** , * ( foo). , * foo. **, FirstName = John, . * foo. **, FirstName = John FirstName.

, , , .

+4

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


All Articles