我在使用一个提交按钮发布包含
这是我的一个模型,张贴的其内具有 多个部分视图的视图的问题多局部视图的视图,
:更新时间:
public class AModel
{
public int AID { get; set; }
public int BID { get; set; }
public int CID { get; set; }
public int ANumber { get; set; }
public BModel BModel { get; set; }
public CModel CModel { get; set; }
}
这是我的B型
public class BModel
{
public int BID { get; set; }
public int BName { get; set; }
public IList<DModel> DModel { get; set; }
}
THI s是d模型
public class DModel
{
public int DID { get; set; }
public int? Number { get; set; }
}
这是C型号
public class CModel
{
public int CID { get; private set; }
public IList<HModel> HModels { get; set; }
}
等 这是主视图
@model Project.Web.Models.AModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.Partial("CreateB",Model.BModel);
@Html.Partial("CreateC",Model.CModel);
<input style="border-radius:4px;width:100px;" type="button" value="next" id="next" class="btn btn-default" />
}
这是获取动作
public ActionResult Create(){
Models.AModel model = new Models.AModel(){
BModel = new BModel(){
DModel = new List<Models.DModel>(){ new Models.DModel() },
CModel = new CModel(){
HModel = new List<HModel>(){ new Models.HModel() },
};
return View(model);
}
这是Post acti在
[HttpPost]
public ActionResult Create(Models.AModel model)
{
//doing things with data
//when I reach this point all object are null
return RedirectToAction("index", model);
}
public void SetNumber(Models.BModel model){
model.DModel.Add(new Models.DModel());
}
局部视图BModel和CModel类似于BModel局部视图 注:SetNumber方法只创建一个新的对象,并将其添加到列表中
@model Project.Web.Models.BModel
@Html.TextBoxItemFor(x => x.BName)
@for (int i=0; i< Model.DModel.Count; i++){
@Html.TextBoxItemFor(x => x.DModel[i].Number)
<a id="addNumber" href="/AController/SetNumber/" data-ajax="true" data-
ajax-method="GET" >Add Another Number</a>
}
我能做些什么?我错过了什么?
除非您将主模型传递给该方法,否则您不能使用'@ Html.Partial()',或者指定'HtmlFieldPrefix'(请参阅[此答案](http://stackoverflow.com/questions/29808573/)得到最值-从-A-嵌套复杂对象 - 即-IS-传递到一个偏视图/ 29809907#29809907))。但正确的做法是为子模型使用'EditorTemplate' –