title: Binding radio buttons in ASP.NET MVC page with razor
body: <p>I have a page with 4 radio buttons (Exam question and 4 options to choose from). With the code below, I can read the option values after user clicks the button. </p>
  
  <p>I have two questions:</p>
  
  <ul>
  <li>Why are all radio buttons selected when the page first loads?</li>
  <li>How to find out which radio button is selected after the post?</li>
  </ul>
  
  <p>Thanks!</p>
  
  <p>StudentQuestions.cshtml</p>
  
  <pre><code>@page "{examId:int?}"
  @model VerityLearn.WebUI.Pages.Questions.StudentQuestionsModel
  @{
      ViewData["Title"] = "StudentQuestions";
  }
      &lt;div&gt;
          &lt;form method="post"&gt;
              @Html.LabelFor(model =&gt; model.QuestionViewModel.QuestionText);
  
                  &lt;p&gt;Select the correct option.&lt;/p&gt;
  
                  int optionIndex = -1;
  
                  @foreach (OptionViewModel opt in Model.QuestionViewModel.Options)
                  {
                      optionIndex++;
  
                      @Html.RadioButtonFor(model =&gt; model.QuestionViewModel.Options[optionIndex], Model.QuestionViewModel.Options[optionIndex]);
                      @opt.OptionText&lt;br /&gt;
                  }
  
              &lt;button type="submit" asp-route-questionIndex="@(Model.QuestionNdx + 1)" class="btn btn-primary @nextDisabled"&gt;Next&lt;/button&gt;
  
          &lt;/form&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>StudentQuestions.cshtml.cs</p>
  
  <pre><code>   public class StudentQuestionsModel : PageModel
      {
          //private readonly VerityContext _context;
          private readonly SignInManager&lt;VerityUser&gt; _signInManager;
          private readonly UserManager&lt;VerityUser&gt; _userManager;
          private readonly IStudentQuesionsUOW _studentQuesionsUOW;
          private readonly VerityLearn.DataAccess.VerityContext _context;
  
          public StudentQuestionsModel(
              VerityContext context,
              SignInManager&lt;VerityUser&gt; signInManager,
              UserManager&lt;VerityUser&gt; userMrg,
              IStudentQuesionsUOW studentQuesionsUOW
          )
          {
              _context = context;
              _signInManager = signInManager;
              _userManager = userMrg;
              _studentQuesionsUOW = studentQuesionsUOW;
          } // end public StudentQuestionsModel(VerityContext context, SignInManager&lt;VerityUser&gt; signInManager, UserManager&lt;VerityUser&gt; userMrg)
  
          [BindProperty]
          public QuestionViewModel QuestionViewModel { get; set; }
  
          [BindProperty]
          public Question Question { get; set; }
  
          public async Task&lt;IActionResult&gt; OnPostAsync()
          {
              ... Not sure what to do here ...
          }
  </code></pre>
  
  <p>QuestionViewModel.cs</p>
  
  <pre><code>    public class QuestionViewModel
      {
          public int QuestionId { get; set; }
  
          public int ExamQuestionOrder { get; set; }
  
          public string QuestionText { get; set; }
  
          public bool? IsSingleSelection { get; set; }
  
          public List&lt;OptionViewModel&gt; Options { get; set; }
      } 
  </code></pre>