我创建了一个局部视图(即使是编辑器模板),我将一个子模型传递给视图,但是,当我单击“提交”时,我总是从局部视图中得到“null”。除了子模型之一,我可以获得主模型的属性值。
主要型号
public class PetModel
{
public string name {get; set;}
public long SpeciesID {get; set;}
public long BreedID {get; set;}
public Calendar DOB {get; set;}
}
子模型
public class Calendar
{
public string Year{get; set;}
public string Month{get; set;}
public string Day{get; set;}
}
主视图
@model Application.Models.PetModel
@using (Html.BeginForm("CatchPetContent", "Quote",Model))
{
@Html.TextBoxFor(x => x.Name)
@Html.DropDownListFor(x=>x.SpeciesID,new List<SelectListItem>(),"select")
@Html.DropDownListFor(x=>x.BreedID,new List<SelectListItem>(),"select")
@Html.EditorFor(Model => x.DOB)
<input type="submit" value="submit" />
}
编辑器模板
@model Application.Models.Calendar
@Html.DropDownListFor(Model => Model.Day, new List<SelectListItem>())
@Html.DropDownListFor(Model => Model.Month,new List<SelectListItem>())
@Html.DropDownListFor(Model => Model.Year, new List<SelectListItem>())
“CatchPetContent”操作
[HttpPost]
public ActionResult CatchPetContent(PetModel Model)
{
PetModel pet = new PetModel();
pet.Name = Model.Name;
pet.SpeciesID = Model.SpeciesID;
pet.BreedID = Model.BreedID;
pet.DOB = Model.DOB;// always null
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("Controller", "Home");
redirectTargetDictionary.Add("Action", "Index");
return new RedirectToRouteResult(new RouteValueDictionary(redirectTargetDictionary));
}
当我调试它时,“Model.DOB”总是为空