我有一个outerViewModel,里面有两个ViewModel, 当我尝试绑定内部模型时,我为所有属性获取null ...
这是代码:
**Models.cs**
public class OuterModel
{
public FirstInnerModel firstInnerModel;
public SecondInnerModel secondInnerModel;
}
public class FirstInnerModel
{
public string Title;
}
public class SecondInnerModel
{
public string Title;
}
Index.cshtml
@using (Html.BeginForm("ActivateFirst", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.firstInnerModel.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.firstInnerModel.Title)
@Html.ValidationMessageFor(model => model.firstInnerModel.Title)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
HomeController.cs
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
var model = new OuterModel()
{
firstInnerModel = new FirstInnerModel(),
secondInnerModel = new SecondInnerModel()
};
return View(model);
}
[HttpPost]
public void ActivateFirst(FirstInnerModel ggg)
{
}
ggg.Title返回null ...
任何?帮助!
答案 0 :(得分:3)
当您提交表单时,它会将OuterModel发布到控制器,因此您需要执行以下操作:
[HttpPost]
public void ActivateFirst(OuterModel ggg)
{
var whatever = ggg.FirstInnerModel.Title;
}