Implementation advice for quiz with 4 choice questions
-
Hey, I', looking for some implementation advice. I am creating this ASP.NET MVC page for quizzes that each course may have a different number of questions with 4 choices. I would like to send the questions to my view as my Model, and by submitting the form, my controller endpoint receives the Id of the questions and selected answers. Can you please give me some tips that how to send such information to my controller using the sample code I wrote in the following? Thank you in advance. My Model
public class QuizQuestion
{
[Key]
public int QuestionId { get; set; }
public int CourseId { get; set; }
public int Order { get; set; }
public string Question { get; set; }
public string Choice1 { get; set; }
public string Choice2 { get; set; }
public string Choice3 { get; set; }
public string Choice4 { get; set; }
public int RightAnswer { get; set; }
}My View
using (Html.BeginForm("Submit", "Quiz", FormMethod.Post, new { }))
{
@Html.AntiForgeryToken()
foreach (var item in Model.QuizJustQuestionsDto)
{@(item.Order + ". " + item.Question) @Html.RadioButton(item.QuestionId.ToString(), 1) @Html.Label(item.Choice1) @Html.RadioButton(item.QuestionId.ToString(), 2) @Html.Label(item.Choice2) @Html.RadioButton(item.QuestionId.ToString(), 3) @Html.Label(item.Choice3) @Html.RadioButton(item.QuestionId.ToString(), 4) @Html.Label(item.Choice4) }
-
Hey, I', looking for some implementation advice. I am creating this ASP.NET MVC page for quizzes that each course may have a different number of questions with 4 choices. I would like to send the questions to my view as my Model, and by submitting the form, my controller endpoint receives the Id of the questions and selected answers. Can you please give me some tips that how to send such information to my controller using the sample code I wrote in the following? Thank you in advance. My Model
public class QuizQuestion
{
[Key]
public int QuestionId { get; set; }
public int CourseId { get; set; }
public int Order { get; set; }
public string Question { get; set; }
public string Choice1 { get; set; }
public string Choice2 { get; set; }
public string Choice3 { get; set; }
public string Choice4 { get; set; }
public int RightAnswer { get; set; }
}My View
using (Html.BeginForm("Submit", "Quiz", FormMethod.Post, new { }))
{
@Html.AntiForgeryToken()
foreach (var item in Model.QuizJustQuestionsDto)
{@(item.Order + ". " + item.Question) @Html.RadioButton(item.QuestionId.ToString(), 1) @Html.Label(item.Choice1) @Html.RadioButton(item.QuestionId.ToString(), 2) @Html.Label(item.Choice2) @Html.RadioButton(item.QuestionId.ToString(), 3) @Html.Label(item.Choice3) @Html.RadioButton(item.QuestionId.ToString(), 4) @Html.Label(item.Choice4) }
I found this awesome post about solving my issue: asp.net mvc 3 - Getting a list of radio button values in ASP MVC 3 - Stack Overflow[^]