C# MVC RadioButton关于如何将内容发回控制器(&W)
C和MVC都是新成员,因此提前为发布一些可能很明显的内容表示歉意。我已经查看了类似的答案,但仍然看不到应该如何使用RadioButton中的值,以及该值是什么,以便将其发布回控制器 控制器C# MVC RadioButton关于如何将内容发回控制器(&W),c#,asp.net-mvc-4,radiobuttonfor,C#,Asp.net Mvc 4,Radiobuttonfor,C和MVC都是新成员,因此提前为发布一些可能很明显的内容表示歉意。我已经查看了类似的答案,但仍然看不到应该如何使用RadioButton中的值,以及该值是什么,以便将其发布回控制器 控制器 [HttpPost] public ActionResult Score(ExamViewModel exam) { const int AddCorrect = 1; var correct = from c in db.Answers
[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传递到Score Controller,但没有成功。如果您能指出如何将选中的RadioButton值传递回Controller,我将不胜感激。您不应该有3个不同的属性,而应该有一个包含答案的属性。这将允许您对单选按钮进行分组,并只能选择其中一个:
@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.Answer, "1")
@Html.DisplayFor(model => model.Foil2.Foil2)
@Html.RadioButtonFor(model => model.Answer, "2")
@Html.DisplayFor(model => model.Foil3.Foil3)
@Html.RadioButtonFor(model => model.Answer, "3")
@Html.DisplayFor(model => model.CorrectAnswer.CorrectAnswer)
@Html.RadioButtonFor(model => model.Answer, "4")
<p>
<input type="submit" value="Submit Answers" />
</p>
</fieldset>
}
然后在控制器操作中检查发送的应答属性值是否正确。正如您从视图中看到的,我们对这个问题有多个答案,其值是将发送到服务器的内容。通常,您将使用答案的ID作为值,并在服务器上比较这是否是问题的正确答案