0

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?

Agnes
  • 57
  • 1
  • 7
  • What is you model(s)? You create a collection of your question model and use a `for` loop for each question, and a nested `foreach` loop for the radio buttons. –  May 08 '18 at 22:46
  • @StephenMuecke My model has all these variables to store answers values and a question model. I have a foreach loop for question model and a for loop which traverses through each of the possible answers. I am thinking how can I save the answers (which radio button is selected among these) without all these answer variables. – Agnes May 09 '18 at 13:16
  • You have it the wrong way around. Suggest you study the code in [this answer](https://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) –  May 09 '18 at 22:03

0 Answers0