目前,我有一个带有多个问题的问卷的工作原型,每个问题都有多个答案选择。一切都显示并保存得很好。但是,我现在想在我的编辑视图中将问题/答案分组到“部分”中。我尝试了几种不同的方法,但似乎都没有正常工作。没有节的工作代码如下:
编辑查看进度报告:
<div class="form-group">
@Html.LabelFor(model => model.ReportAnswers, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReportAnswers)
</div>
</div>
ReportAnswers.cshtml(编辑器模板)
<h3>
Question
</h3>
<p>
@Html.DisplayFor(x => x.Question.QuestionText)
</p>
@Html.HiddenFor(model => model.QuestionID)
@Html.HiddenFor(model => model.ReportID)
@foreach (var answer in Model.Question.PossibleAnswers)
{
var id = string.Format("answer-{0}", answer.AnswerID);
<p>
@Html.HiddenFor(model => model.ReportAnswerID)
@Html.RadioButtonFor(m => m.AnswerID, answer.AnswerID, new { id = id })
<label for="@id">@answer.AnswerText</label>
</p>
}
编辑控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ProgressReport progressReport)
{
if (ModelState.IsValid)
{
db.Entry(progressReport).State = EntityState.Modified;
db.SaveChanges();
foreach (ReportAnswer answer in progressReport.ReportAnswers)
{
if (answer.QuestionID != null && answer.AnswerID != null)
{
db.Entry(answer).State = EntityState.Modified;
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
ViewBag.ClientID = new SelectList(db.Clients, "ClientID", "ID", progressReport.ClientID);
return View(progressReport);
}
数据结构是 Sections -> Questions -> Answers 然后我有一个 ProgressReport 表,其中包含 QuestionID 和 AnswerID
我试图在视图中进行 Groupby,但不确定如何正确调用 Editortemplate。其实我是可以用的,但是结果并没有想象中的那么好。该代码是:
@foreach (var group in Model.ReportAnswers.GroupBy(s => s.Question.SectionID))
谢谢!
数据结构片段: