title: @html razor radio buttons mvc5
body: <p>here's my viewmodel</p>
  
  <pre><code>public class UserResponseModel
      {
          public string QuestionId { get; set;} 
  
          public string QuestionText { get; set; }
  
          public bool IsChecked { get; set; }
  
  
      }
  </code></pre>
  
  <p>so, for checkboxes this works beautifully</p>
  
  <pre><code>for (var i = 0; i &lt; Model.UserResponses.Count; i++)
      {
          &lt;div class="row"&gt;
              &lt;div class="col-md-4"&gt;
                  &lt;div class="form-group"&gt;
  
                      @Html.HiddenFor(x =&gt; x.UserResponses[i].QuestionId)
                      @Html.CheckBoxFor(x =&gt; x.UserResponses[i].IsChecked) 
  </code></pre>
  
  <p>but for radio buttons this does not </p>
  
  <pre><code> for (var i = 0; i &lt; Model.UserResponses.Count; i++)
      {
          &lt;div class="row"&gt;
              &lt;div class="col-md-4"&gt;
                  &lt;div class="form-group"&gt;
  
                      @Html.HiddenFor(x =&gt; x.UserResponses[i].QuestionId)
                      @Html.RadioButtonFor(x =&gt; x.UserResponses[i].IsChecked, new { Name = "grp1" })
                      @Html.DisplayTextFor(x =&gt; x.UserResponses[i].QuestionText)
  </code></pre>
  
  <p>when i submit the form then IsChecked is always false - why - what am i missing - as I mentioned for checkboxes it works just fine. I did look at this question <a href="https://stackoverflow.com/questions/17987360/asp-net-mvc-razor-how-to-show-grouped-radio-buttons-for-two-model-fields">here</a> but i'm not sure why radiobuttons require extra properties in the view model to hold the correct answer when checkboxes work transparently. </p>
  
  <p>Edit: my question model is now as so</p>
  
  <pre><code>public class QuestionModel
      {
          public string WhichQuestion { get; set; }
  
          public int PointsObtained { get; set; }
  
          public bool CorrectAnswer { get; set; }
  
          private List&lt;UserResponseModel&gt; _userResponse;
          public List&lt;UserResponseModel&gt; UserResponses 
          {
              get { return _userResponse ?? (_userResponse = new List&lt;UserResponseModel&gt;()); }
              set { _userResponse = value; }
          }
      }
  </code></pre>
  
  <p>notice i have just added public bool CorrectAnswer { get; set; }</p>
  
  <p>and in my view here's the code</p>
  
  <pre><code>for (var i = 0; i &lt; Model.UserResponses.Count; i++)
      {
          &lt;div class="row"&gt;
              &lt;div class="col-md-4"&gt;
                  &lt;div class="form-group"&gt;
  
                      @Html.HiddenFor(x =&gt; x.UserResponses[i].QuestionId)
                      @Html.HiddenFor(x =&gt; x.CorrectAnswer)
                      @Html.HiddenFor(x =&gt; x.UserResponses[i].IsChecked)
                      @Html.RadioButtonFor(x =&gt; x.UserResponses[i].IsChecked, Model.CorrectAnswer, new { Name = "grp1" })
  </code></pre>
  
  <p>EDIT 2: </p>
  
  <pre><code> @Html.HiddenFor(x =&gt; x.UserResponses[i].QuestionId)
                      @Html.HiddenFor(x =&gt; x.SelectedAnswerId)
                      @Html.HiddenFor(x =&gt; x.UserResponses[i].IsChecked)
                      @Html.RadioButtonFor(x =&gt; x.UserResponses[i].IsChecked,    Model.SelectedAnswerId, new { Name = "grp1" })
  </code></pre>
  
  <p>EDIT3:</p>
  
  <pre><code>public class QuestionModel
      {
          public string WhichQuestion { get; set; }
  
          public int PointsObtained { get; set; }
  
  
  
          private List&lt;UserResponseModel&gt; _userResponse;
          public List&lt;UserResponseModel&gt; UserResponses 
          {
              get { return _userResponse ?? (_userResponse = new List&lt;UserResponseModel&gt;()); }
              set { _userResponse = value; }
          }
      }
  
  public class UserResponseModel
      {
          public string QuestionId { get; set;} 
  
          public string QuestionText { get; set; }
  
  
  
          public string SelectedQuestionId { get; set; }
  
      }
  </code></pre>
  
  <p>finally, my view</p>
  
  <pre><code>for (var i = 0; i &lt; Model.UserResponses.Count; i++)
      {
          &lt;div class="row"&gt;
              &lt;div class="col-md-4"&gt;
                  &lt;div class="form-group"&gt;
  
                      @Html.HiddenFor(x =&gt; x.UserResponses[i].QuestionId)
                      @Html.RadioButtonFor(x =&gt; x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId, new { Name = "grp1" })
                      @*@Html.CheckBoxFor(x =&gt; x.UserResponses[i].IsChecked)*@
                      @Html.DisplayTextFor(x =&gt; x.UserResponses[i].QuestionText)
  </code></pre>
  
  <p>EDIT4:so finally I'm getting some where 
  this works!!!</p>
  
  <pre><code>@Html.RadioButtonFor(x =&gt; x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId)
  </code></pre>
  
  <p>I can now see the selectedquestionid populated in my httppost method but if I do this</p>
  
  <pre><code>@Html.RadioButtonFor(x =&gt; x.UserResponses[i].SelectedQuestionId, Model.UserResponses[i].QuestionId, new {Name="grp"}) 
  </code></pre>
  
  <p>then though i am able to select only one radiobutton  the selectedquestionid <em>is</em> null on httppost - wierd</p>