从单选按钮组读取数据 asp.net mvc
Reading data from radio button Group asp.net mvc
我正在尝试做一个多选题测验
我制作了这个 Quiz View 模型,这样讲师就可以对课程进行测验并选择正确答案
public class QuizMV
{
public int CourseIDD { get; set; }
public int InstructorID { get; set; }
public string title { get; set; }
public int QuizMark { get; set; }
public IList<Question> questions { get; set; }
}
public partial class Question
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
//public Question()
//{
// this.Answers = new HashSet<Answer>();
//}
public int QuizID { get; set; }
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public int QuestionMark { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual IList<Answer> Answers { get; set; }
public virtual Quiz Quiz { get; set; }
}
public partial class Answer
{
public int QuestionID { get; set; }
public int AnswerID { get; set; }
public string AnswerText { get; set; }
public bool CorrectAnswer { get; set; }
public int QuizID { get; set; }
public virtual Question Question { get; set; }
}
现在我试图让学生做这个测验并选择他自己的答案,但答案没有显示在发送给控制器的模型中
for (var i = 0; i< Model.questions.Count(); i++) {
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name" style="font-weight: bolder;font-size: x-large;color: #a757e8;">Question #@(i + 1):</label>
<label style="color: #a757e8;font-size: larger;">(Mark: @Html.DisplayFor(m => m.questions[i].QuestionMark))</label>
<div class="row">
<div class="col-md-12" style="font-size: 20px;font-weight:bold;">
@Html.HiddenFor(m => m.questions[i].QuestionID) @Html.DisplayFor(m => m.questions[i].QuestionText)
</div>
</div>
</div>
</div>
@for (var j = 0; j< Model.questions[i].Answers.Count(); j++) { <div class="col-md-6">
<div class="form-group">
<div style="display:inline-block">
@Html.RadioButtonFor(m=>m.questions[i].Answers[i].CorrectAnswer,"true") @Html.DisplayFor(m => m.questions[i].Answers[j].AnswerText) @Html.HiddenFor(m => m.questions[i].Answers[j].AnswerText)
</div>
</div>
</div>
}
</div>
}
谁能帮我访问控制器中的答案?
如果你想将选定的 AnswerText 传递给控制器,你可以这样做:
型号(添加SelectedAnswerText
):
public partial class Question
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
//public Question()
//{
// this.Answers = new HashSet<Answer>();
//}
public int QuizID { get; set; }
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public int QuestionMark { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual IList<Answer> Answers { get; set; }
public virtual Quiz Quiz { get; set; }
public string SelectedAnswerText { get; set; }
}
控制器:
[HttpGet]
public IActionResult TestQuizMV() {
QuizMV q = new QuizMV { questions = new List<Question> { new Question { QuestionID=1, QuestionMark=3, QuestionText="q1",Answers=new List<Answer> { new Answer { AnswerID=1, AnswerText="A",CorrectAnswer=true}, new Answer { AnswerID = 2, AnswerText = "B", CorrectAnswer = false }, new Answer { AnswerID = 3, AnswerText = "C", CorrectAnswer = false } } },
new Question { QuestionID=2, QuestionMark=2, QuestionText="q2",Answers=new List<Answer> { new Answer { AnswerID=1, AnswerText="A",CorrectAnswer=false}, new Answer { AnswerID = 2, AnswerText = "B", CorrectAnswer = true }, new Answer { AnswerID = 3, AnswerText = "C", CorrectAnswer = false } } },
new Question { QuestionID=3, QuestionMark=3, QuestionText="q3",Answers=new List<Answer> { new Answer { AnswerID=1, AnswerText="A",CorrectAnswer=false}, new Answer { AnswerID = 2, AnswerText = "B", CorrectAnswer = false }, new Answer { AnswerID = 3, AnswerText = "C", CorrectAnswer = true } } }} };
return View(q);
}
[HttpPost]
public IActionResult TestQuizMV(QuizMV quizMV) {
return Ok();
}
查看:
<form method="post" id="myform">
@for (var i = 0; i < Model.questions.Count(); i++)
{
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name" style="font-weight: bolder;font-size: x-large;color: #a757e8;">Question #@(i + 1):</label>
<label style="color: #a757e8;font-size: larger;">(Mark: @Html.DisplayFor(m => m.questions[i].QuestionMark))</label>
<div class="row">
<div class="col-md-12" style="font-size: 20px;font-weight:bold;">
@Html.HiddenFor(m => m.questions[i].QuestionID) @Html.DisplayFor(m => m.questions[i].QuestionText)
</div>
</div>
</div>
</div>
@for (var j = 0; j < Model.questions[i].Answers.Count(); j++)
{<div class="col-md-6">
<div class="form-group">
<div style="display:inline-block">
@Html.RadioButtonFor(m => m.questions[i].Answers[j].CorrectAnswer,true, new {@class="CorrectAnswer"}) @Html.DisplayFor(m => m.questions[i].Answers[j].AnswerText)
<input class="hiddenSelectedAnswerText" hidden asp-for=@Model.questions[i].Answers[j].AnswerText name="questions[@i].SelectedAnswerText" />
</div>
</div>
</div>
}
</div>
}
<input type="submit" value="submit"/>
</form>
js:
<script>
$("#myform").submit(function () {
$("div[style='display:inline-block']").each(function () {
if ($(this).find(".CorrectAnswer").attr("checked") != "checked") {
var id = $(this).find(".CorrectAnswer").attr("id").replace("CorrectAnswer", "AnswerText");
$("#" + id).remove();
}
})
})
</script>
结果:
我正在尝试做一个多选题测验 我制作了这个 Quiz View 模型,这样讲师就可以对课程进行测验并选择正确答案
public class QuizMV
{
public int CourseIDD { get; set; }
public int InstructorID { get; set; }
public string title { get; set; }
public int QuizMark { get; set; }
public IList<Question> questions { get; set; }
}
public partial class Question
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
//public Question()
//{
// this.Answers = new HashSet<Answer>();
//}
public int QuizID { get; set; }
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public int QuestionMark { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual IList<Answer> Answers { get; set; }
public virtual Quiz Quiz { get; set; }
}
public partial class Answer
{
public int QuestionID { get; set; }
public int AnswerID { get; set; }
public string AnswerText { get; set; }
public bool CorrectAnswer { get; set; }
public int QuizID { get; set; }
public virtual Question Question { get; set; }
}
现在我试图让学生做这个测验并选择他自己的答案,但答案没有显示在发送给控制器的模型中
for (var i = 0; i< Model.questions.Count(); i++) {
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name" style="font-weight: bolder;font-size: x-large;color: #a757e8;">Question #@(i + 1):</label>
<label style="color: #a757e8;font-size: larger;">(Mark: @Html.DisplayFor(m => m.questions[i].QuestionMark))</label>
<div class="row">
<div class="col-md-12" style="font-size: 20px;font-weight:bold;">
@Html.HiddenFor(m => m.questions[i].QuestionID) @Html.DisplayFor(m => m.questions[i].QuestionText)
</div>
</div>
</div>
</div>
@for (var j = 0; j< Model.questions[i].Answers.Count(); j++) { <div class="col-md-6">
<div class="form-group">
<div style="display:inline-block">
@Html.RadioButtonFor(m=>m.questions[i].Answers[i].CorrectAnswer,"true") @Html.DisplayFor(m => m.questions[i].Answers[j].AnswerText) @Html.HiddenFor(m => m.questions[i].Answers[j].AnswerText)
</div>
</div>
</div>
}
</div>
}
谁能帮我访问控制器中的答案?
如果你想将选定的 AnswerText 传递给控制器,你可以这样做:
型号(添加SelectedAnswerText
):
public partial class Question
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
//public Question()
//{
// this.Answers = new HashSet<Answer>();
//}
public int QuizID { get; set; }
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public int QuestionMark { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual IList<Answer> Answers { get; set; }
public virtual Quiz Quiz { get; set; }
public string SelectedAnswerText { get; set; }
}
控制器:
[HttpGet]
public IActionResult TestQuizMV() {
QuizMV q = new QuizMV { questions = new List<Question> { new Question { QuestionID=1, QuestionMark=3, QuestionText="q1",Answers=new List<Answer> { new Answer { AnswerID=1, AnswerText="A",CorrectAnswer=true}, new Answer { AnswerID = 2, AnswerText = "B", CorrectAnswer = false }, new Answer { AnswerID = 3, AnswerText = "C", CorrectAnswer = false } } },
new Question { QuestionID=2, QuestionMark=2, QuestionText="q2",Answers=new List<Answer> { new Answer { AnswerID=1, AnswerText="A",CorrectAnswer=false}, new Answer { AnswerID = 2, AnswerText = "B", CorrectAnswer = true }, new Answer { AnswerID = 3, AnswerText = "C", CorrectAnswer = false } } },
new Question { QuestionID=3, QuestionMark=3, QuestionText="q3",Answers=new List<Answer> { new Answer { AnswerID=1, AnswerText="A",CorrectAnswer=false}, new Answer { AnswerID = 2, AnswerText = "B", CorrectAnswer = false }, new Answer { AnswerID = 3, AnswerText = "C", CorrectAnswer = true } } }} };
return View(q);
}
[HttpPost]
public IActionResult TestQuizMV(QuizMV quizMV) {
return Ok();
}
查看:
<form method="post" id="myform">
@for (var i = 0; i < Model.questions.Count(); i++)
{
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name" style="font-weight: bolder;font-size: x-large;color: #a757e8;">Question #@(i + 1):</label>
<label style="color: #a757e8;font-size: larger;">(Mark: @Html.DisplayFor(m => m.questions[i].QuestionMark))</label>
<div class="row">
<div class="col-md-12" style="font-size: 20px;font-weight:bold;">
@Html.HiddenFor(m => m.questions[i].QuestionID) @Html.DisplayFor(m => m.questions[i].QuestionText)
</div>
</div>
</div>
</div>
@for (var j = 0; j < Model.questions[i].Answers.Count(); j++)
{<div class="col-md-6">
<div class="form-group">
<div style="display:inline-block">
@Html.RadioButtonFor(m => m.questions[i].Answers[j].CorrectAnswer,true, new {@class="CorrectAnswer"}) @Html.DisplayFor(m => m.questions[i].Answers[j].AnswerText)
<input class="hiddenSelectedAnswerText" hidden asp-for=@Model.questions[i].Answers[j].AnswerText name="questions[@i].SelectedAnswerText" />
</div>
</div>
</div>
}
</div>
}
<input type="submit" value="submit"/>
</form>
js:
<script>
$("#myform").submit(function () {
$("div[style='display:inline-block']").each(function () {
if ($(this).find(".CorrectAnswer").attr("checked") != "checked") {
var id = $(this).find(".CorrectAnswer").attr("id").replace("CorrectAnswer", "AnswerText");
$("#" + id).remove();
}
})
})
</script>
结果: