我正在做一个 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 模型类的强类型视图,我不知道为什么所有回发的值都是空的,这只发生在嵌套模型中。我错过了什么吗?
谢谢你帮助我
@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>
}