我在使用一個提交按鈕發佈包含
這是我的一個模型,張貼的其內具有 多個部分視圖的視圖的問題多局部視圖的視圖,
:更新時間:
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' –