At the moment, I have a working prototype for a questionnaire with multiple questions and each question having multiple choices for an answer. Everything displays and saves great. However, I now would like to group the question/answers into 'sections' on my Edit view. I have tried a couple different methods but nothing seems to work correctly. The working code without sections are as follows:
Edit View Progress Report:
<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 (editor template)
<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>
}
EditController:
[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);
}
Data structure is Sections -> Questions -> Answers I then have a ProgressReport table that has QuestionID and AnswerID
I have attempted to Groupby within the view, but then unsure how to call the Editortemplate correctly. In fact, I was able to use it, but the result was not as expected. that code is:
@foreach (var group in Model.ReportAnswers.GroupBy(s => s.Question.SectionID))
Thanks!
Data Structure Snippet:
I was able to develop a solution...proper or not..Don't know...but it does work. Feel free to critique. I welcome it. :)
ProgressReportViewModel:
EditController:
ReportAnswers Editor Template
Edit View:
I chose to do these arrays because there is one section at least I need to intercept to do something special with. Otherwise all other sections are typical. This seems to work but as stated, I welcome any criticisms anyone may have.
Have a great day!