我正在尝试创建一个调查页面,其中包含文本框和单选按钮列表或复选框字段。无论我尝试什么,我都无法在提交表单时获得要绑定的model.Questions属性;使用null Questions属性创建模型。
请告诉我你有一些想法可以帮助我!
视图模型如下所示:
// Survey view model
public class Question
{
public int Id { set; get; }
public string QuestionText { set; get; }
public bool IsHeading { get; set; }
public bool IsList { get; set; }
public bool IsCheckBox { get; set; }
public List<Answer> Answers { set; get; }
[Required]
public string SelectedAnswer { set; get; }
public string GivenAnswer { get; set; }
public Question()
{
Answers = new List<Answer>();
GivenAnswer = "-1";
SelectedAnswer = "-1";
QuestionText = "";
}
}
public class Answer
{
public int Id { set; get; }
public string AnswerText { set; get; }
}
public class Assessment
{
public int Id { get; set; }
public string Name { get; set; }
public int LessonId { get; set; }
public Module Lesson { get; set; }
public SurveyType SurveyType { get; set; }
public virtual List<Question> Questions { set; get; }
public Assessment()
{
Questions = new List<Question>();
}
}
控制器方法如下所示:
[HttpPost]
public ActionResult Survey(Assessment model)
{
if (ModelState.IsValid)
{
foreach (var q in model.Questions)
{
var qId = q.Id;
var selectedAnswer = q.SelectedAnswer;
// Save the data
}
return RedirectToAction("ThankYou", new { id = model.Id }); //PRG Pattern
}
//to do : reload questions and answers
return View(model);
}
Assessment
是主视图模型。以下是视图(Survey.cshtml)和编辑器模板(EditorTemplates / Question.cshtml,每个问题)
Survey.cshtml:
@model ZTTDD.Models.Assessment
@using ZTTDD.Models
<div class="row">
<div class="col-md-9">
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.Id)
@Html.EditorFor(m => m.Questions)
<input type="submit" />
}
</div>
<div class="col-md-3">
<p>
@Html.ActionLink((string)ViewBag.LessonLinkBackText, "Lesson", "Lessons", new { id = Model.Lesson.Id }, null)
</p>
<p>
@Html.ActionLink(linkName, linkMethod, "Lessons", new { id = Model.Lesson.Id }, null)
</p>
</div>
</div>
EditorTemplates / Question.cshtml
@model ZTTDD.Models.Question
@if (Model.IsHeading)
{
<div class="survey-heading">
<h4>@Model.QuestionText</h4>
</div>
<hr/>
}
@if (!Model.IsHeading)
{
<div class="form-group">
@Html.HiddenFor(m => m.Id)
<p> @Model.QuestionText </p>
@if (Model.IsList)
{
<div class="radio">
@Html.HiddenFor(m => m.GivenAnswer)
@foreach (var a in Model.Answers)
{
if (Model.IsCheckBox)
{
<span class="form-horizontal">
@Html.CheckBox(String.Format("Questions_{0}__SelectedAnswer", Model.Id), false, new { value = "", name = String.Format("Questions[{0}].SelectedAnswer", Model.Id) }) @Html.Raw(a.AnswerText)
</span>
}
else
{
@Html.RadioButtonFor(b => b.SelectedAnswer, a.Id) @Html.Raw(a.AnswerText) <br />
}
}
</div>
}
@if (!Model.IsList)
{
Model.GivenAnswer = "";
@Html.HiddenFor(m => m.SelectedAnswer)
@Html.TextAreaFor(m => m.GivenAnswer)
}
</div>
}
以下是发布的密钥列表:
[0] "Id" string
[1] "Questions[1].Id" string
[2] "Questions[1].GivenAnswer" string
[3] "Questions[1].SelectedAnswer" string
[4] "Questions[2].Id" string
[5] "Questions[2].GivenAnswer" string
[6] "Questions[2].SelectedAnswer" string
[7] "Questions[3].Id" string
[8] "Questions[3].GivenAnswer" string
[9] "Questions[3].SelectedAnswer" string
[10] "Questions[5].Id" string
[11] "Questions[5].GivenAnswer" string
[12] "Questions[5].SelectedAnswer" string
[13] "Questions[6].Id" string
[14] "Questions[6].GivenAnswer" string
[15] "Questions[6].SelectedAnswer" string
[16] "Questions[7].Id" string
[17] "Questions[7].GivenAnswer" string
[18] "Questions[7].SelectedAnswer" string
[19] "Questions[9].Id" string
[20] "Questions[9].SelectedAnswer" string
[21] "Questions[9].GivenAnswer" string
[22] "Questions[10].Id" string
[23] "Questions[10].SelectedAnswer" string
[24] "Questions[10].GivenAnswer" string
更新:
稍微澄清一下:一个Question对象可以表示一个标题(没有关联的字段)或输入(单选按钮,复选框或textarea)。据我所知,我需要的值正确发布。我将尝试从调试器中获取它们并在此处发布。
更新:
下面的查询字符串来自Request.Form.ToString(),带有url-decoding和格式,便于阅读。正如您所看到的,值是发布的,但由于某种原因,不会受到Assessment.Questions的约束。这实际上是由于问题[xx]中的xx值,因为它不是索引值,而是实际的Id?
Id=1&
Questions[1].Id=2&
Questions[1].GivenAnswer=-1&
Questions[1].SelectedAnswer=2&
Questions[2].Id=3&
Questions[2].GivenAnswer=-1&
Questions[2].SelectedAnswer=5&
Questions[3].Id=5&
Questions[3].GivenAnswer=-1&
Questions[3].SelectedAnswer=8&
Questions[5].Id=7&
Questions[5].GivenAnswer=-1&
Questions[5].SelectedAnswer=12&
Questions[6].Id=8&
Questions[6].GivenAnswer=-1&
Questions[6].SelectedAnswer=15&
Questions[7].Id=9&
Questions[7].GivenAnswer=-1&
Questions[7].SelectedAnswer=18&
Questions[9].Id=11&
Questions[9].SelectedAnswer=-1&
Questions[9].GivenAnswer=sdfg sdfg dsfg&
Questions[10].Id=12&
Questions[10].SelectedAnswer=-1&
Questions[10].GivenAnswer=sdfg dfsg sdfg
最后更新:
问题在于,我没有将任何内容发回服务器以查找标题问题,因此问题索引实际上是不完整的。在每个标题中添加隐藏的Id字段可以解决问题。
答案 0 :(得分:3)
我设置了一个基本项目,该项目利用了您发布的代码示例,以及一些基本的虚拟数据,以及问题集合的约束。
我猜那时候您正在渲染的特定调查的数据中存在导致问题的内容。
查看代码,我可以看到两个可能的问题:
Question.IsHeader
成立时,我认为任何内容都不会回复到此问题的表单中。我想那时模型活页夹没有为这个问题拿起任何东西。从你的表单数据来看,它看起来就是这样 - 它缺少0,4和8的索引。我实际上并不知道这是否会导致模型绑定器失败,但它可以做到。您可以尝试在问题编辑器模板的HiddenFor
为真的部分中为问题ID添加IsHeading
。 @Html.CheckBox
代码使用问题的ID格式化输入值,而不是列表中问题的索引。我猜这也会破坏绑定。希望如果你能解决这些问题,那么绑定就可以了。我建议在没有任何标题问题的情况下进行调查,没有任何复选框或单选按钮问题,并查看绑定是否有效。