I have some set of questions and each question have 3 options which user can select only one among 3. (radio button) And I want to save the answer selected also.
What I did is like below.
foreach (tblQuestion question in Model.TechQuestions)
{
<p> @question.QuestionDescription</p>
@for (int i = 0; i < Model.TechAnswers.Count(); i++)
{
if (question.QuestionID == 5)
{
<div class="col-md-3 col-sm-3 text-left">
@Html.RadioButtonFor(m => Model.selectedAnswerIDforQ5, Model.TechAnswers[i].AnswerID)
</div>
}
if (question.QuestionID == 6)
{
<div class="col-md-3 col-sm-3 text-left">
@Html.RadioButtonFor(m => Model.selectedAnswerIDforQ6, Model.TechAnswers[i].AnswerID)
</div>
}
if (question.QuestionID == 7)
{
<div class="col-md-3 col-sm-3 text-left">
@Html.RadioButtonFor(m => Model.selectedAnswerIDforQ7, Model.TechAnswers[i].AnswerID)
</div>
}
if (question.QuestionID == 8)
{
<div class="col-md-3 col-sm-3 text-left">
@Html.RadioButtonFor(m => Model.selectedAnswerIDforQ8, Model.TechAnswers[i].AnswerID)
</div>
}
}
}
Model.TechAnswers.Count() is 3 so I will have 3 radio buttons and selected answers id (AnswerID) will be saved in variables selectedAnswerIDforQ5 for QuestionID 5 , selectedAnswerIDforQ6 for QuestionID 6 ,selectedAnswerIDforQ7 for QuestionID 7 and so on. This is working fine. But when my application gets bigger, suppose I have 200 questions, I will have 200 variables to store all question answers.
Is there another way of saving the values of each question other than using these variables? Like an array or dictionary or something? So I don't have to repeat the code like above. (Like checking the question ID and radio button for each of the question)
I want answers of all questions in Model.TechQuestions to get saved somewhere with less no of code. Another way than above, or is this a good way?