0

I'm creating a simple survey using ASP.NET MVC wherein the questions are answerable by 3 options - Strongly agree, somewhat agree, strongly disagree. The user have to select his answer by selecting a radiobutton.

This is how my survey looks like

Right now, I can only select one radiobutton for the whole table. I wish to select a RadioButton for each row in the table, for a user to select his answer for every corresponding question.

This is my ViewModel:

public class PsychTestViewModel
    {
        //PsychTestQuestion
        public int QuestionID { get; set; }
        public string Question { get; set; }

        //PsychTest
        public string UserID { get; set; }
        public int Answer { get; set; }
    }

My View:

<table class="table">
    <tr>
        <th>
            Question
        </th>
        <th class="text-center">
            Strongly Agree
        </th>
        <th class="text-center">
            Somewhat Agree
        </th>
        <th class="text-center">
            Strongly Disagree
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Question)
            </td>
            <td align="center">
                @Html.RadioButtonFor(modelItem => item.Answer, "1")
            </td>
            <td align="center">
                @Html.RadioButtonFor(modelItem => item.Answer, "2")
            </td>
            <td align="center">
                @Html.RadioButtonFor(modelItem => item.Answer, "3")
            </td>
        </tr>
    }

</table>

How can I group these RadioButtons so that the user can select one radiobutton for every row?

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • Not only that, but it would never bind to any thing when you submitted your form! You need to use a `for` loop –  Jan 05 '18 at 20:19
  • Can you please provide me an example I can follow? – f0rtuna Jan 05 '18 at 20:25
  • https://stackoverflow.com/questions/14165632/asp-net-mvc-4-for-loop-posts-model-collection-properties-but-foreach-does-not – JamieD77 Jan 05 '18 at 20:28
  • Refer [this answer](https://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) for an example –  Jan 05 '18 at 20:49

0 Answers0