【发布时间】:2016-08-03 13:40:37
【问题描述】:
我在发布包含多个部分视图的视图时遇到问题 使用一个提交按钮
这是我的 A 模型,
:更新:
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; }
}
这是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);
}
这是发布操作
[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());
}
Partial View For BModel,CModel类似于BModel的Partial view 注意:SetNumber 方法只创建一个新的 Object 并将其添加到列表中
@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(参考this answer)。但正确的做法是为子模型使用EditorTemplate
标签: c# asp.net asp.net-mvc