EditorTemplates在调查问卷上的位置或分组

时间:2015-06-10 15:11:05

标签: asp.net-mvc-5 mvc-editor-templates editortemplates

目前,我有一个问题调查表的工作原型,其中包含多个问题,每个问题都有多个答案选择。一切都显示和保存很好。但是,我现在想将问题/答案分组到'部分'在我的编辑视图上。我尝试了几种不同的方法,但似乎没有任何正常工作。没有部分的工作代码如下:

修改视图进度报告:

<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>
}

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);
    }

数据结构是章节 - &gt;问题 - &gt;答案 然后我有一个ProgressReport表,其中包含QuestionID和AnswerID

我在视图中尝试过Groupby,但后来不确定如何正确调用Editortemplate。事实上,我能够使用它,但结果并不像预期的那样。那段代码是:

@foreach (var group in Model.ReportAnswers.GroupBy(s => s.Question.SectionID))

谢谢!

数据结构代码段 enter image description here

2 个答案:

答案 0 :(得分:1)

您最好创建表示要在视图中显示的数据的视图模型

public class ReportVM
{
  public int ID { get; set; }
  // other properties or Report for display/editing
  public List<SectionVM> Sections { get; set; }
}
public class SectionVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  // other properties or Report for display/editing
  public List<QuestionVM> Questions{ get; set; }
}
public class QuestionVM
{
  public int ID { get; set; }
  public string Title { get; set; }
  public int SelectedAnswer { get; set; }
  public List<Answer> PossibleAnswers { get; set; }
}

在控制器中,使用.GroupBy()查询构建视图模型并添加每个部分及其相关问题/答案

然后为EditorTempate

添加SectionVM
@model SectionVM
@Html.HiddenFor(m = > m.ID)
<h2>@Html.DisplayFor(m => m.Name)</h2>
@Html.EditorFor(m => m.Questions) // uses an EditorTemplate for QuestionVM (i.e. as per your current ReportAnswers template

并在主视图中

@Html.EditorFor(model => model.Sections)

答案 1 :(得分:-1)

我能够开发出一种解决方案......不管是不是......不知道......但确实有效。随意批评。我欢迎它。 :)

ProgressReportViewModel:

public class ProgressReportViewModel
{
    public int ReportID { get; set; }
    public DateTime ReportDateSubmitted {get; set;}
    public List<ReportAnswer>[] ReportAnswerSection { get; set; }
    public string[] SectionHeadings { get; set; }
    public string[] SectionDescriptions { get; set; }
}

EditController:

public ActionResult Edit(int? id)
    {
        var viewModel = new ProgressReportViewModel();

       //  Get the section in order of defined display order
        var questionsection = (from s in db.QuestionSections
                                orderby s.SectionDisplayOrder
                                select new{
                                    s.SectionName,
                                    s.SectionDesc
                                });

        viewModel.SectionHeadings = new string[11];
        viewModel.SectionDescriptions = new string[11];
        viewModel.ReportAnswerSection = new List<ReportAnswer>[11];

        int i = 0;
        foreach(var section in questionsection)
        {
            //  Loop through sections and get the name, desc and the questions/answers
            viewModel.SectionHeadings[i] = section.SectionName.ToString();
            viewModel.SectionDescriptions[i] =   section.SectionDesc;
            viewModel.ReportAnswerSection[i] = db.ReportAnswers.Where(q => q.Question.SectionID == i+1 && q.ReportID == id).Include(s => s.Question.QuestionSection).OrderBy(q => q.Question.DisplayOrder).ToList();
            i=i+1;
        }

        var report = (from r in db.ProgressReports
                      where r.ReportID == id
                      select new
                      {
                          r.ReportID,
                          r.ReportDateSubmitted,
                          r.ClientID
                      }).SingleOrDefault();
        viewModel.ReportID = report.ReportID;
        viewModel.ReportDateSubmitted = report.ReportDateSubmitted ?? DateTime.MinValue;


        return View(viewModel);
    }



    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(ProgressReportViewModel progressReport)
    {
        if (ModelState.IsValid)
        {
            ProgressReport PR = db.ProgressReports.Where(x => x.ReportID == progressReport.ReportID).SingleOrDefault();


            //db.ProgressReports.Attach(progressReport);

            db.Entry(PR).State = EntityState.Modified;
            db.SaveChanges();

            for (var i = 0; i <= 10; i++ )
            {
                foreach (ReportAnswer answer in progressReport.ReportAnswerSection[i]) //.ReportAnswers)
                {
                    SaveAnswers(answer);
                }
            }

            return RedirectToAction("Index", "Home");
        }
        return View(progressReport);
    }

ReportAnswers编辑器模板

<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>
}

修改视图:

@for(var i = 0; i<=10; i++)
    {
        <h4>@Html.DisplayFor(model => model.SectionHeadings[i]) </h4>
        <p>@Html.DisplayFor(model => model.SectionDescriptions[i]) </p>
        @Html.EditorFor(model => model.ReportAnswerSection[i])
    }

我选择做这些数组,因为至少我需要拦截一个部分来做一些特别的事情。否则所有其他部分都是典型的。 这似乎有效,但如上所述,我欢迎任何人可能提出任何批评。

祝你有美好的一天!