[英]Reading data from radio button Group asp.net mvc
I'm trying to make a quiz with multiple choice answers I made this Quiz View model so the instructor can make a quiz for a course and choose the correct answers我正在尝试使用多项选择答案进行测验 我制作了这个测验视图 model 以便教师可以对课程进行测验并选择正确答案
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; } }
Now I'm trying to make the student make this quiz and choose his own answers, but the answers are not showing in the model sent to the controller现在我试图让学生做这个测验并选择他自己的答案,但答案没有显示在发送到 controller 的 model 中
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> }
Can anyone help me to Access the answers in the controller?谁能帮我访问 controller 中的答案?
If you want to pass selected AnswerText to controller,you can do like this:如果要将选定的 AnswerText 传递给 controller,可以这样做:
Model(add SelectedAnswerText
):模型(添加
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; }
}
Controller: Controller:
[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();
}
View:看法:
<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: 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.