C# 和 MVC 的新手,因此提前为发布可能很明显的内容道歉。我看过类似的答案,但仍然看不到应该如何使用 RadioButtonFor 中的值以及应该使用什么值,以便可以将其发布回控制器。
控制器
[HttpPost]
public ActionResult Score(ExamViewModel exam)
{
const int AddCorrect = 1;
var correct = from c in db.Answers
where c.ID == 1
select c.CorrectAnswer;
if (ModelState.IsValid)
{
if (correct == exam.CorrectAnswer)
{
ViewData["message"] = "Correct Answer!";
return View("Results");
}
else
{
var feedback = from g in db.Questions
where g.ID == 1
select g.GrammarPoint;
ViewData["message"] = "That's not the right answer.";
ViewData["feedback"] = feedback;
return View("Results");
}
}
return View("Results");
和视图
@model AccessEsol.Models.ExamViewModel
@{
ViewBag.Title = "TakeTest";
}
<h2>TakeTest</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="display-label">
<h3>Click the correct answer:</h3>
</div>
<div class="display-field">
<strong>@Html.DisplayFor(model => model.Text.Text )</strong>
</div>
@Html.DisplayFor(model => model.Foil1.Foil1)
@Html.RadioButtonFor(model =>model.Foil1, "Incorrect" )
@Html.DisplayFor(model => model.Foil2.Foil2)
@Html.RadioButtonFor(model => model.Foil2, "Incorrect" )
@Html.DisplayFor(model => model.Foil3.Foil3)
@Html.RadioButtonFor(model => model.Foil3, "Incorrect")
@Html.DisplayFor(model => model.CorrectAnswer.CorrectAnswer)
@Html.RadioButtonFor(model => model.CorrectAnswer, "Correct")
<p>
<input type="submit" value="Submit Answers" />
</p>
</fieldset>
}
我还尝试将 CorrectAnswer 中的字符串传递给分数控制器,但没有成功。如果您能指出如何将检查的 RadioButton 值传递回控制器,将不胜感激?