私は ASP.NET MVC プロジェクトを行っています。次のようにネストされたモデルがあります。
public class A
.........
public class B
.............
public class AB
{
public A _a;
public B _b;
public AB()
{
_a = new A();
_b = new B();
}
}
そしてコントローラーで:
public ActionResult Create()
{
AB model = new AB();
return View(model);
}
[HttpPost]
public ActionResult Create(AB abModel)
{
//all properties of abModel._a and abModel._b are null
return View(abModel);
}
私のビューは、AB モデル クラスの厳密に型指定されたビューです。ポスト バックされるすべての値が null である理由がわかりません。これは、ネストされたモデルでのみ発生します。何か不足していますか?
私を助けてくれてありがとう
@reneが提案したようにモデルを更新
public class AB
{
public A _a {get; set};
public B _b {get; set};
public AB()
{
_a = new A();
_b = new B();
}
}
コードを表示:
@model TestMVC.AB
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<table cellspacing="0" cellpadding="0" class="forms">
<tbody>
<tr><th>
@Html.LabelFor(model => model._a.ClientName)
</th>
<td>
@Html.TextBoxFor(model => model._a.ClientName, new { @class = "inputbox"})
@Html.ValidationMessageFor(model => model._a.ClientName)
</td></tr>
<tr><th></th><td><input type="submit" value="Create" /></td></tr>
</tbody></table>
}