从分组ViewModel的细节中创建下拉列表
本文关键字:创建 下拉列表 细节 ViewModel | 更新日期: 2023-09-27 18:07:14
我从MVC实体框架模型中的几个表创建了一个新模型。
namespace Prototype_07.Models
{
public class MyQuestionModel
{
public Questionaire Questionaire { get; set; }
public QuestionaireSection QuestionaireSection { get; set; }
public Section Section { get; set; }
public SectionQuestion SectionQuestion { get; set; }
public Question Question { get; set; }
public QuestionType QuestionType { get; set; }
public QuestionAnswerListCode QuestionAnswerListCode { get; set; }
public AnswerListCode AnswerListCode { get; set; }
}
}
我的控制器代码是:
公共类MyQuestionController: Controller{private PrototypeEntities db = new PrototypeEntities();
public ActionResult Index()
{
var viewModel = from qa in db.Questionaires
join qas in db.QuestionaireSections on qa.QuestionaireKey equals qas.QuestionaireKey
join s in db.Sections on qas.SectionKey equals s.SectionKey
join sq in db.SectionQuestions on s.SectionKey equals sq.SectionKey
join q in db.Questions on sq.QuestionKey equals q.QuestionKey
join qtc in db.QuestionTypes on q.QuestionTypeKey equals qtc.QuestionTypeKey
join qddl in db.QuestionAnswerListCodes on q.QuestionKey equals qddl.QuestionKey
join ddl in db.AnswerListCodes on qddl.AnswerListCodeKey equals ddl.AnswerListCodeKey
where qa.QuestionaireName.Equals("TAD")
select new MyQuestionModel
{
Questionaire = qa,
QuestionaireSection = qas,
Section = s,
SectionQuestion = sq,
Question = q,
QuestionType = qtc,
QuestionAnswerListCode = qddl,
AnswerListCode = ddl
};
return View(viewModel);
}
我想创建一个如下图所示的视图
在我看来,我被如何将问题分组并填充下拉列表所困扰。MVC是怎么做的?
<table>
<tr>
<th>@Html.DisplayFor(model => Model.First().Questionaire.QuestionaireName)</th>
</tr>
@foreach (var item in Model.OrderBy(x => x.QuestionaireSection.DefaultSequence).OrderBy(x => x.SectionQuestion.DefaultSequence).OrderBy(x => x.QuestionAnswerListCode.DefaultSequence)) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Section.SectionName))
</td>
<td>
@Html.DisplayFor(modelItem => item.Question.Question1)
</td>
<td>
@Html.DropDownListFor(??????)
</td>
</tr>
}
</table>
试试这个:
@Html.DropDownListFor(modelItem => item.QuestionAnswerListCode.AnswerList, @Model.AnswerListCode)