当表格最初显示时,我会显示问题和答案。我显示了多个单选按钮以供回答。因此用户只能选择一个答案。当我提交表单和操作方法调用时,会出现问题。我看到表单帖子然后答案模型变为空。因此,指导我如何编写代码,因为当表单发布时,答案模型不应为null。
这是完整的代码。请仔细阅读代码,如果需要更改答案模型将不会为空,此时表单将发布到操作方法。
我的ViewModels代码
namespace ViewModels
{
public class Question
{
public int ID { set; get; }
public string QuestionText { set; get; }
public List<Answer> Answers { set; get; }
[Required]
public string SelectedAnswer { set; get; }
public Question()
{
Answers = new List<Answer>();
}
}
public class Answer
{
public int ID { set; get; }
public string AnswerText { set; get; }
}
public class Evaluation
{
public List<Question> Questions { set; get; }
public Evaluation()
{
Questions = new List<Question>();
}
}
}
controller code
public ActionResult Index()
{
var evalVM = new Evaluation();
//the below is hardcoded for DEMO. you may get the data from some
//other place and set the questions and answers
var q1 = new Question { ID = 1, QuestionText = "What is your favourite language" };
q1.Answers.Add(new Answer { ID = 12, AnswerText = "PHP" });
q1.Answers.Add(new Answer { ID = 13, AnswerText = "ASP.NET" });
q1.Answers.Add(new Answer { ID = 14, AnswerText = "Java" });
evalVM.Questions.Add(q1);
var q2 = new Question { ID = 2, QuestionText = "What is your favourite DB" };
q2.Answers.Add(new Answer { ID = 16, AnswerText = "SQL Server" });
q2.Answers.Add(new Answer { ID = 17, AnswerText = "MySQL" });
q2.Answers.Add(new Answer { ID = 18, AnswerText = "Oracle" });
evalVM.Questions.Add(q2);
return View(evalVM);
}
[HttpPost]
public ActionResult Index(Evaluation model)
{
if (ModelState.IsValid)
{
foreach (var q in model.Questions)
{
if(q.Answers==null)
{
// Answers is null
}
var qId = q.ID;
var selectedAnswer = q.SelectedAnswer;
// Save the data
}
return RedirectToAction("ThankYou"); //PRG Pattern
}
//reload questions
return View(model);
}
index.cshtml code
@model ViewModels.Evaluation
<h2>Quiz 24</h2>
@using (Html.BeginForm())
{
@Html.EditorFor(x=>x.Questions)
<input type="submit" />
}
and view code which is stored in EditorTemplates folder
@model ViewModels.Question
<div>
@Html.HiddenFor(x=>x.ID)
<h3> @Model.QuestionText </h3>
@foreach (var a in Model.Answers)
{
<p>
@Html.RadioButtonFor(b=>b.SelectedAnswer,a.ID) @a.AnswerText
</p>
}
</div>
问题出在这里
[HttpPost]
public ActionResult Index(Evaluation model)
{
if (ModelState.IsValid)
{
foreach (var q in model.Questions)
{
if(q.Answers==null)
{
// Answers is null
}
var qId = q.ID;
var selectedAnswer = q.SelectedAnswer;
// Save the data
}
return RedirectToAction("ThankYou"); //PRG Pattern
}
//reload questions
return View(model);
}
q.Answers == null在我发布表单时变为null。我想知道如何在表单将发布到操作时以这种方式编写代码然后Answers应该为null。 很多人告诉我,我需要手动重建模型,因为Answers将始终为null。 MVC中没有任何机制可以保留所有数据,并在发布表单时将其正确地反序列化为模型。
答案 0 :(得分:0)
AnswerText通常我会建议为Editor Template
模型使用另一个嵌套Answer
,但在这种情况下,它会导致问题正确构建单选按钮组,并且aftwerwards将检索{{1}的值}}。作为一个简单的解决方案,我将通过以下方式调整SelectedAnswer
Editor Template
模型:
Question
这将创建以下HTML(跳过不相关的属性):
<div>
@Html.HiddenFor(x=>x.ID)
<h3> @Model.QuestionText </h3>
<div>
@for (int i = 0; i < Model.Answers.Count; i++)
{
<p>
@Html.RadioButtonFor(model => model.SelectedAnswer, Model.Answers[i].AnswerText @Model.Answers[i].AnswerText
@Html.HiddenFor(model => model.Answers[i].ID)
@Html.HiddenFor(model => model.Answers[i].AnswerText)
</p>
}
</div>
</div>
输出将允许MVC正确重建<input type="radio" name="Questions[0].SelectedAnswer" ...>
<input type="hidden" name="Questions[0].Answers[0].ID" ...>
<input type="hidden" name="Questions[0].Answers[0].AnswerText" ...>
集合。
<强>更新强>
在原始代码中,您正在寻找Answer
,因此我更新了代码,而不是Answer.AnswerText
。
希望这有帮助。