我做的ASP.NET MVC項目,我有如下嵌套模型: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>
}
我試過,但沒有工作:( – davidcoder
你能添加視圖的HTML標記? – rene
我更新的觀點,你要求 – davidcoder