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
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
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?
If you want to pass selected AnswerText to controller,you can do like this:
Model(add 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:
[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:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.