関連する質問がいくつかありますが、うまくいく答えが見つかりません。
次のモデルがあると仮定します。
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;}
}
そして、次のことを行う拡張ビューViewPage<EditorViewModel>
:
<%= Html.TextBoxFor(model => model.Account.AccountName)%>
<%= Html.ValidationMessageFor(model => model.Account.AccountName)%>
<%= Html.TextBoxFor(model => model.SomeSimpleStuff )%>
<%= Html.ValidationMessageFor(model => model.SomeSimpleStuff )%>
そして私のコントローラーは次のようになります:
[HttpPost]
public virtual ActionResult Edit(EditorViewModel account)
{ /*...*/ }
DefaultModelBinder を取得して EditorViewModel を適切にバインドするにはどうすればよいですか? 特別なことを何もせずに、すべてが null またはデフォルトの EditorViewModel の空のインスタンスを取得します。
私が最も近いのは、UpdateModel
手動で呼び出すことです:
[HttpPost]
public virtual ActionResult Edit(EditorViewModel account)
{
account.Account = new Account();
UpdateModel(account.Account, "Account");
// this kills me:
UpdateModel(account);
これにより Account プロパティ モデルが正常に更新されますが、account
(EditorViewModel の残りのパブリック プロパティを取得するために) UpdateModel を呼び出すと、まったく役に立たない「タイプのモデル ... を更新できませんでした」というメッセージが表示されます。内部例外がないため、何が問題なのかわかりません。
これをどうすればいいですか?