【发布时间】:2015-06-19 12:22:02
【问题描述】:
我正在尝试为测试制作动态表格。 我的问题是当我提交时,控制器列表 gq 中的 post 方法为空,但我希望填充表单中填写的数据。 我可以猜测它必须与@Html.EditorForModel() 和数据绑定有关,但我不完全理解它是如何工作的。任何的想法?谢谢
型号:
public class GeneratedQuestions
{
public int GeneratedQuestionsId { get; set; }
public string QuestionText { get; set; }
public List<StudentAnswer> QuestionAnswers { get; set; }
public int StudentTestsId { get; set; }
public virtual StudentTest StudentTests { get; set; }
}
查看:
@model IEnumerable<Project.Models.GeneratedQuestions>
@{
ViewBag.Title = "StartTest";
}
@using (Html.BeginForm())
{
@Html.EditorForModel()
foreach (var question in Model)
{
<br />
<h1>@question.QuestionText</h1>
foreach (var answer in question.QuestionAnswers)
{
@Html.CheckBoxFor(x => answer.WasChecked)
@Html.DisplayFor(x => answer.AnswerText)
@Html.HiddenFor(x => answer.GeneratedQuestionsId)
<br />
}
}
<input type="submit" value="Submit" />
}
控制器:
public ActionResult StartTest(int id)
{
List<GeneratedQuestions> generatedQuestions = db.StudentTests.Find(id).GeneratedQuetions.ToList();
return View(generatedQuestions);
}
[HttpPost]
public ActionResult StartTest(List<GeneratedQuestions> gq)
{
//stuff
return RedirectToAction("Index");
}
【问题讨论】:
-
您需要使用
for而不是foreach。 Here 只是关于可枚举模型和表单的许多其他类似问题之一。
标签: c# asp.net-mvc forms post