-1

現時点では、複数の質問があり、各質問に複数の回答選択肢があるアンケートの実用的なプロトタイプがあります。すべてが表示され、保存されます。ただし、編集ビューで質問/回答を「セクション」にグループ化したいと思います。いくつかの異なる方法を試しましたが、何も正しく機能していないようです。セクションのない作業コードは次のとおりです。

ビュー進行状況レポートの編集:

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

データ構造は Sections -> Questions -> Answers です 次に、QuestionID と AnswerID を持つ ProgressReport テーブルがあります

ビュー内で Groupby を試みましたが、Editortemplate を正しく呼び出す方法がわかりません。実際、使用できましたが、結果は期待どおりではありませんでした。そのコードは次のとおりです。

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

ありがとう!

データ構造スニペット: ここに画像の説明を入力

4

2 に答える 2

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

エディットコントローラー:

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

これらの配列を選択したのは、何か特別なことを行うためにインターセプトする必要があるセクションが少なくとも 1 つあるためです。それ以外の場合は、他のすべてのセクションが標準です。これは機能しているように見えますが、述べたように、誰かが持っているかもしれない批判を歓迎します.

すてきな一日を!

于 2015-06-12T20:34:33.033 に答える