title: Radio button validation before post
body: <p>I want to ensure that a user picks an answer to the question being asked. I have tried JQuery, Custom Validation class, setting model attributes to required. To summarise, if no radio button is selected, then the error message is shown and the user has to select an answer before progressing to the next question.</p>
  
  <p>This is the HTML/Razor without the ValidationMessageFor, as it hasn't worked.</p>
  
  <pre><code>@using (Html.BeginForm("Question", "PreRegistration", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  {
   @if (Model.Questions.fldAnswerType == "Radio")
    {
      &lt;form id="myForm" name="myForm"&gt;
          &lt;div class="form-group"&gt;
              &lt;div style="margin-left: 20px;"&gt;
                  @{
                      if (Model.Questions.fldOption1 != null)
                      {
                          &lt;div class="radio"&gt;
                              @Html.RadioButton("Radio", @Model.Questions.fldOption1, new { @class = "myGroup" }) @Model.Questions.fldOption1                             
                          &lt;/div&gt;
                      }
  
                      if (Model.Questions.fldOption2 != null)
                      {
                          &lt;div class="radio"&gt;
                              @Html.RadioButton("Radio", @Model.Questions.fldOption2, new { @class = "myGroup" }) @Model.Questions.fldOption2                             
                          &lt;/div&gt;
                      }
  
                      if (Model.Questions.fldOption3 != null)
                      {
                          &lt;div class="radio"&gt;
                              @Html.RadioButton("Radio", @Model.Questions.fldOption3, new { @class = "myGroup" }) @Model.Questions.fldOption3
                          &lt;/div&gt;
                      }
  
                      if (Model.Questions.fldOption4 != null)
                      {
                          &lt;div class="radio"&gt;
                              @Html.RadioButton("Radio", @Model.Questions.fldOption4) @Model.Questions.fldOption4
                          &lt;/div&gt;
                      }
  
                      if (Model.Questions.fldOption5 != null)
                      {
                          &lt;div class="radio"&gt;
                              @Html.RadioButton("Radio", @Model.Questions.fldOption5) @Model.Questions.fldOption5
                          &lt;/div&gt;
                      }
  
                      if (Model.Questions.fldOption6 != null)
                      {
                          &lt;div class="radio"&gt;
                              @Html.RadioButton("Radio", @Model.Questions.fldOption6) @Model.Questions.fldOption6
                          &lt;/div&gt;
  
                      }
                  }
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;input type="submit" value="Next" id="btn" class="btn btn-primary" /&gt;
  </code></pre>
  
  <p>I have spent all day trying to get it to work but I can't...</p>
  
  <pre><code>[HttpPost]
  public ActionResult Question(FormCollection collection, Questions questions)
  {
     if (!ModelState.IsValid)
     {
       var viewModel = new RegistrationViewModel
        {
          Questions = questions,
          Responses = response
        };
  
       return View("Question", viewModel);
     }
   }
  </code></pre>
  
  <p>The question Model I am posting back is always null, so I am assuming this is the reason why the model state is always valid?</p>
  
  <p>Any pointers would be appreciated</p>