如何使用BeginCollectionItem绑定嵌套列表(Parent,Children,Grand children)?我能够绑定父和子,但是Grand Children集合是0.在FireBug上,我能够看到SurveyAnswers(大孩子),但由于某种原因,他们没有绑定到SurveyQuestions(Answers的父级) 。 Survey是SurveyQuestions的父母。
创建视图
@model Survey
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "requestTypeForm" }))
{
@Html.AntiForgeryToken()
@Html.Partial("_RequestTypeForm", Model)
}
_RequestTypeForm Partial View
@model Survey
@using (Html.BeginForm(null, null, FormMethod.Post))
{
@Html.TextBoxFor(model => model.Name)
<div id="questions"></div>
<button id="btnAddQuestion" type="button" />
}
_questionForm Partial View
@model SurveyQuestion
@using (Html.BeginCollectionItem("SurveyQuestions"))
{
@Html.TextBoxFor(model => model.Question)
<div id="answers"></div>
<button id="btnAddAnswerToQuestion" type="button" />
}
_answerForm部分视图
@model SurveyAnswer
@using (Html.BeginCollectionItem("SurveyAnswers"))
{
@Html.TextBoxFor(model => model.Answer)
}
制作行动
public ActionResult Create()
{
var model = Survey.New();
return View(model);
}
问题部分查看操作
public PartialViewResult CreateSurveyQuestionView()
{
return PartialView("_questionForm", SurveyQuestion.New(true)); // Marking as child
}
回答部分查看操作
public PartialViewResult CreateSurveyAnswerView()
{
return PartialView("_answerForm", SurveyAnswer.New(true)); // Marking as child
}
创建后期操作
[HttpPost]
public ActionResult Create(Survey request)
{
request.Save();
}