【发布时间】:2014-06-05 15:44:23
【问题描述】:
我有一个用于调查的 VS2012、MVC 4、Entity Framework 4.0 项目。我很难想出一种方法将结果返回给我的控制器。大多数答案将来自下拉列表,每个问题一个。我正在使用视图将表格展平为一个简洁的对象,其中包含我在视图中需要的所有信息。 SQL 视图如下所示:
SELECT TOP (100) PERCENT
Questionaires.QuestionaireKey,
Questionaires.QuestionaireName,
Sections.SectionKey,
Sections.SectionName,
QuestionaireSections.DefaultSequence AS QAS_Sequence,
Questions.QuestionKey,
Questions.Question,
SectionQuestions.DefaultSequence AS SQ_Sequence,
QuestionTypes.QuestionTypeKey,
QuestionTypes.QuestionTypeName,
AnswerListCodes.AnswerListCodeKey,
AnswerListCodes.AnswerListCodeName,
QuestionAnswerListCodes.DefaultSequence AS QALC_Sequence
FROM dbo.Questionaires AS Questionaires
INNER JOIN dbo.QuestionaireSections AS QuestionaireSections
ON Questionaires.QuestionaireKey = QuestionaireSections.QuestionaireKey
INNER JOIN dbo.Sections AS Sections
ON QuestionaireSections.SectionKey = Sections.SectionKey
INNER JOIN dbo.SectionQuestions AS SectionQuestions
ON Sections.SectionKey = SectionQuestions.SectionKey
INNER JOIN dbo.Questions AS Questions
ON SectionQuestions.QuestionKey = Questions.QuestionKey
INNER JOIN dbo.QuestionTypes AS QuestionTypes
ON Questions.QuestionTypeKey = QuestionTypes.QuestionTypeKey
INNER JOIN dbo.QuestionAnswerListCodes AS QuestionAnswerListCodes
ON Questions.QuestionKey = QuestionAnswerListCodes.QuestionKey
INNER JOIN dbo.AnswerListCodes AS AnswerListCodes
ON QuestionAnswerListCodes.AnswerListCodeKey = AnswerListCodes.AnswerListCodeKey
每个问题都有来自 AnswerListCodes 的可能答案。我了解 vw_Questions 实体是只读的。需要保存的数据位于 Answers & EvaluationAnswers 表中。到目前为止,我的控制器代码是:
public ActionResult Index()
{
ViewBag.Answers = db.Answers.ToList();
ViewBag.EvaluationAnswers = db.EvaluationAnswers.ToList();
return View(db.vw_Questions.ToList());
}
这导致display that I'm looking 在这里。
我没有看到如何将每个下拉列表中的选定答案返回到我的控制器。有些答案也会是文本。
查看代码:
@model IEnumerable<Prototype_06.Models.vw_Questions>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<style>
table { border-collapse:collapse; }
table,th, td {border: 1px solid gray; }
</style>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<table>
@foreach (var group in (Model.OrderBy(x => x.QAS_Sequence).GroupBy(item => item.SectionName)))
{
<tr>
<th colspan="3">@group.Key</th>
</tr>
foreach (var item in group.OrderBy(x => x.SQ_Sequence).GroupBy(subItem => subItem.Question).Distinct()) {
<tr>
<td> </td>
<td>@item.Key</td>
<td>
<select>
<option value="@Guid.Empty"></option>
@foreach (var ans in item.OrderBy(x => x.QALC_Sequence))
{
<option value="@ans.AnswerListCodeKey">@ans.AnswerListCodeName</option>
}
</select>
</td>
</tr>
}
}
</table>
<p>
<input type="submit" value="Save" />
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
【问题讨论】:
-
所以你希望一旦用户做出选择,控制器就会知道它并返回一个新的视图模型。我猜?
-
还是一头雾水。提交表单后,它将发布到表单的
action属性中指定的控制器操作。那么,您能否向我们展示您视图中的代码? -
我认为一个关键部分是找到一种方法来以 MVC 方式呈现下拉列表。我认为这将允许所选结果显示在发布的数据中
-
好吧,我想你离理解 MVC 还很远。视图模型不是这样构建的。请去检查一个最基本的教程。它会帮助你。
-
我查看了许多基本示例,它们只处理单个表,我了解它们是如何工作的。我的问题是显示来自许多表的数据并保存到视图中不需要显示的表中。
标签: c# asp.net-mvc entity-framework visual-studio-2012 model-view-controller