Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 5 EditorTemplates在调查问卷上显示Where或Groupby_Asp.net Mvc 5_Mvc Editor Templates_Editortemplates - Fatal编程技术网

Asp.net mvc 5 EditorTemplates在调查问卷上显示Where或Groupby

Asp.net mvc 5 EditorTemplates在调查问卷上显示Where或Groupby,asp.net-mvc-5,mvc-editor-templates,editortemplates,Asp.net Mvc 5,Mvc Editor Templates,Editortemplates,目前,我有一个调查问卷的工作原型,有多个问题,每个问题都有多个答案选择。一切显示和保存都非常好。但是,我现在想在编辑视图中将问题/答案分组为“部分”。我尝试了两种不同的方法,但似乎没有一种是正确的。无章节的工作代码如下: 编辑视图进度报告: <div class="form-group"> @Html.LabelFor(model => model.ReportAnswers, htmlAttributes: new { @class = "control-label

目前,我有一个调查问卷的工作原型,有多个问题,每个问题都有多个答案选择。一切显示和保存都非常好。但是,我现在想在编辑视图中将问题/答案分组为“部分”。我尝试了两种不同的方法,但似乎没有一种是正确的。无章节的工作代码如下:

编辑视图进度报告:

<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>
数据结构为节->问题->答案 然后我有一个ProgressReport表,其中有QuestionID和AnswerID

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

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

数据结构片段:

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

公共类ReportVM
{
公共int ID{get;set;}
//用于显示/编辑的其他属性或报告
公共列表节{get;set;}
}
公共类分区VM
{
公共int ID{get;set;}
公共字符串名称{get;set;}
//用于显示/编辑的其他属性或报告
公共列表问题{get;set;}
}
公开课问题
{
公共int ID{get;set;}
公共字符串标题{get;set;}
public int SelectedAnswer{get;set;}
公共列表可能的应答{get;set;}
}
在控制器中,使用
.GroupBy()
查询构建视图模型,并添加每个部分及其相关问题/答案

然后为
SectionVM

@model SectionVM
@Html.HiddenFor(m=>m.ID)
@DisplayFor(m=>m.Name)
@EditorFor(m=>m.Questions)//为QuestionVM使用一个EditorTemplate(即根据您当前的ReportAnswers模板
在主要观点中

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

我能够开发出一个解决方案……正确与否……不知道……但它确实有效。请随意评论。我欢迎。:)

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; }
}
公共类ProgressReportViewModel
{
public int ReportID{get;set;}
public DateTime ReportDateSubmitted{get;set;}
公共列表[]ReportAnswerSection{get;set;}
公共字符串[]节标题{get;set;}
公共字符串[]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);
    }
public ActionResult编辑(int?id)
{
var viewModel=新的ProgressReportViewModel();
//按定义的显示顺序获取节
var questionsection=(从db.QuestionSections中的s开始)
orderby s.SectionDisplayOrder
选择新的{
s、 部门名称,
s、 章节说明
});
viewModel.sectionheaders=新字符串[11];
viewModel.SectionDescriptions=新字符串[11];
viewModel.ReportAnswerSection=新列表[11];
int i=0;
foreach(问题部分中的var部分)
{
//循环浏览各个部分,获取名称、描述和问题/答案
viewModel.sectionheaders[i]=section.SectionName.ToString();
viewModel.SectionDescriptions[i]=section.SectionDesc;
viewModel.ReportAnswerSection[i]=db.ReportAnswers.Where(q=>q.Question.SectionID==i+1&&q.ReportID==id)。包括(s=>s.Question.QuestionSection)。OrderBy(q=>q.Question.DisplayOrder)。ToList();
i=i+1;
}
var报告=(来自db.ProgressReports中的r
其中r.ReportID==id
选择新的
{
r、 ReportID,
r、 提交的报告日期:,
r、 客户
}).SingleOrDefault();
viewModel.ReportID=report.ReportID;
viewModel.ReportDateSubmitted=report.ReportDateSubmitted??DateTime.MinValue;
返回视图(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果编辑(ProgressReportViewModel progressReport)
{
if(ModelState.IsValid)
{
ProgressReport PR=db.ProgressReports.Where(x=>x.ReportID==ProgressReport.ReportID).SingleOrDefault();
//db.ProgressReports.附件(progressReport);
db.Entry(PR.State=EntityState.Modified;
db.SaveChanges();
for(变量i=0;i x.Question.QuestionText)

@Html.HiddenFor(model=>model.QuestionID) @Html.HiddenFor(model=>model.ReportID) @foreach(模型中的var答案。问题。可能答案) { var id=string.Format(“answer-{0}”,answer.AnswerID); @Html.HiddenFor(model=>model.ReportAnswerID) @RadioButton(m=>m.AnswerID,AnswerID,new{id=id}) @答案

}
编辑视图:

@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])
    }
(变量i=0;i模型.sectionheaders[i]) @DisplayFor(model=>model.SectionDescriptions[i])

@Html.EditorFor(model=>model.ReportAnswerSection[i]) } 我选择使用这些数组是因为我至少需要截取一个部分来做一些特殊的处理,否则所有其他部分都是典型的。 这似乎有效,但正如我所说的,我欢迎任何人提出任何批评


祝您愉快!

您是说我应该放弃目前使用的现有编辑器模板吗?另外,我可以使用{get;set;}在使用db first应用程序的viewmodel中?我想我一直认为这是针对使用model first应用程序的模型本身?或者这可能是一个愚蠢的问题?此外,我想我没有考虑在控制器中使用.GroupBy()。我想我可以做到这一点?您使用
编辑模板
是很好的(并且更易于阅读)
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);
    }
<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])
    }