title: How to persist model data when form post
body: <p>When form is showing initially then I show question and their answer. I show multiple radio button for answer. So user can select only one answer. The problem occurs when I submit my form and action method calls. I saw when form post then <strong>answer model is getting null</strong>. So guide me how to write code as a result answer model should not be null when form will post.</p>
  
  <p>Here is full code. Please go through the code and if requires changes as a result <strong>answer model will not be null</strong> when form will be posted to action method.</p>
  
  <p><strong>my ViewModels code</strong></p>
  
  <pre><code>namespace ViewModels
  {
      public class Question
      {
          public int ID { set; get; }
          public string QuestionText { set; get; }
  
          public List&lt;Answer&gt; Answers { set; get; }
          [Required]
          public string SelectedAnswer { set; get; }
  
          public Question()
          {
              Answers = new List&lt;Answer&gt;();
          }
      }
  
      public class Answer
      {
          public int ID { set; get; }
          public string AnswerText { set; get; }
      }
  
      public class Evaluation
      {
          public List&lt;Question&gt; Questions { set; get; }
          public Evaluation()
          {
              Questions = new List&lt;Question&gt;();
          }
      }
  }
  
  controller code
  
   public ActionResult Index()
          {
              var evalVM = new Evaluation();
  
              //the below is hardcoded for DEMO. you may get the data from some  
              //other place and set the questions and answers
  
              var q1 = new Question { ID = 1, QuestionText = "What is your favourite language" };
              q1.Answers.Add(new Answer { ID = 12, AnswerText = "PHP" });
              q1.Answers.Add(new Answer { ID = 13, AnswerText = "ASP.NET" });
              q1.Answers.Add(new Answer { ID = 14, AnswerText = "Java" });
              evalVM.Questions.Add(q1);
  
              var q2 = new Question { ID = 2, QuestionText = "What is your favourite DB" };
              q2.Answers.Add(new Answer { ID = 16, AnswerText = "SQL Server" });
              q2.Answers.Add(new Answer { ID = 17, AnswerText = "MySQL" });
              q2.Answers.Add(new Answer { ID = 18, AnswerText = "Oracle" });
              evalVM.Questions.Add(q2);
  
              return View(evalVM);           
          }
  
   [HttpPost]
          public ActionResult Index(Evaluation model)
          {
              if (ModelState.IsValid)
              {
                  foreach (var q in model.Questions)
                  {
                      if(q.Answers==null)
                      {
                          // Answers is null
                      }
                      var qId = q.ID;
                      var selectedAnswer = q.SelectedAnswer;
                      // Save the data 
  
                  }
                  return RedirectToAction("ThankYou"); //PRG Pattern
              }
              //reload questions
              return View(model);
          }
  
  index.cshtml code
  
  @model ViewModels.Evaluation
  &lt;h2&gt;Quiz 24&lt;/h2&gt;
  @using (Html.BeginForm())
  {
      @Html.EditorFor(x=&gt;x.Questions)
      &lt;input type="submit" /&gt;
  }
  
  and view code which is stored in EditorTemplates folder
  
  @model ViewModels.Question
  &lt;div&gt;
      @Html.HiddenFor(x=&gt;x.ID)
      &lt;h3&gt; @Model.QuestionText &lt;/h3&gt;
  
      @foreach (var a in Model.Answers)
      {
          &lt;p&gt;
              @Html.RadioButtonFor(b=&gt;b.SelectedAnswer,a.ID)  @a.AnswerText 
          &lt;/p&gt;
  
      }
  &lt;/div&gt;
  </code></pre>
  
  <p>the problem is here</p>
  
  <pre><code> [HttpPost]
          public ActionResult Index(Evaluation model)
          {
              if (ModelState.IsValid)
              {
                  foreach (var q in model.Questions)
                  {
                      if(q.Answers==null)
                      {
                          // Answers is null
                      }
                      var qId = q.ID;
                      var selectedAnswer = q.SelectedAnswer;
                      // Save the data 
  
                  }
                  return RedirectToAction("ThankYou"); //PRG Pattern
              }
              //reload questions
              return View(model);
          }
  </code></pre>
  
  <p>q.Answers==null is getting null when i post the form. i like to know the trick that how to write code in such a way when form will be post to action then Answers should be null.
  many guy told me that i need to rebuild the model manually because Answers will be always null. is there any no mechanism in MVC to persist all the data and properly De-serialize it to model when form will be posted.</p>