title: Maintain Checked Radio Group after Post
body: <p>How can I maintain not only the questions and answers for a radio group, but the checked radio as well after a post that returns an error?</p>
  
  <p>The Contact page returns a list of multiple choice questions. If there is an error, the view does not contain the answers or the selected values.</p>
  
  <p>I have searched around, but not found an answer that quite puts it all together. This one is close: <a href="https://stackoverflow.com/questions/4190866/how-do-i-transfer-viewmodel-data-between-post-requests-in-asp-net-mvc">How do I transfer ViewModel data between POST requests in ASP.NET MVC?</a></p>
  
  <p>but I cannot believe there is not an easier solution four years later. </p>
  
  <p>Ken says:</p>
  
  <blockquote>
    <p>you can grab the currently selected item (i.e. validation failed) from
    the property containing the id that was posted and bound to the
    instance of your class.</p>
  </blockquote>
  
  <p>...which is what I want to do essentially: persist the checked radios. But I have to think that there is a way do this is without binding classes or setting hidden values.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public ActionResult Contact()
      {
          SomeViewModel vm = new SomeViewModel();
          vm.Questions = workflow.GetQuestions().ToList();
          vm.Answers = workflow.GetPossibleAnswers();
          return View(vm);//populated radio group
      }
  
  [HttpPost]
  public ActionResult SendForm(FormCollection form)
      {
          if (String.IsNullOrEmpty(form["FullName"]))
          {
              ModelState.AddModelError("FullName", "Must enter a name,");
  
              SomeViewModel vm = new SomeViewModel();
              vm.Questions = workflow.GetQuestions().ToList();
              vm.Answers = workflow.GetPossibleAnswers();
              List&lt;SelectedAnswer&gt; selectedAnswers = ParseSelectedAnswers(form);
              vm.PreviousAnswers = selectedAnswers;
              return View(vm);//returns without the previously selected radios
          }
      //more code…
      }
  
  public class SomeViewModel : RegisterExternalLoginModel
  {
      public List&lt;Question&gt; Questions { get; set; }
      public IList&lt;Answer&gt; PossibleAnswers { get; set; }
      public List&lt;SelectedAnswer&gt; SelectedAnswers { get; set; }
      public IList&lt;SelectedAnswer&gt; PreviousAnswers 
      { 
          set 
          { 
              foreach(Question q in Questions)
              {
                  q.SelectedAnswers = value.Where(t =&gt; t.questionId == q.objectId).ToList() ;
              }
          } 
      }
  }
  </code></pre>
  
  <p>Parse Method</p>
  
  <pre><code>private  List&lt;SelectedAnswer&gt; ParseSelectedAnswers(FormCollection form)
          {
              List&lt;SelectedAnswer&gt; selectedAnswers = new List&lt;SelectedAnswer&gt;();
              var questionElements = form.AllKeys.Where(t =&gt; t.StartsWith("Questions["));
              var uniqueQuestionElements = questionElements.Select(t=&gt; t.Substring(0, t.IndexOf(']') +1)).Distinct();
              var count = uniqueQuestionElements.Count();
  
              foreach(var element in uniqueQuestionElements)
              {
                  long x = ParseElementToLong(form, element, "ObjectId");
                  long y = ParseElementToLong(form, element, "SelectedAnswer");
  
                  selectedAnswers.Add(new SelectedAnswer() { questionId = x, answerId = y });            
              }
              return selectedAnswers;
          }
  </code></pre>
  
  <p>So, the problem is that the view is coming back after the ModelError without any questions, answers, or checked radios.</p>
  
  <p>I am using MVC 5 with Razor.</p>