title: ASP.NET MVC3 fill in form again after post using FormCollection
body: <p>So what I do is, I have a controller that checks if the input in my form is correct. If it's not, it puts an error message in the ViewBag and returns the view. Like this (unimportant stuff left out):</p>
  
  <pre><code>var name = formCollection.Get(cr.Category.name + "-name-" + i).Trim();
  var dateOfBirth = formCollection.Get(cr.Category.name + "-dateOfBirth-" + i).Trim();
  var passportno= formCollection.Get(cr.Category.name + "-passportno-" + i).Trim();
  
  if (name.Equals("") || dateOfBirth.Equals("") || passportno.Equals(""))
  {
       ViewBag.ErrorMessage = "Please fill in all required fields at " + cr.Category.naam;
       return View("BulletinForm3");
  }
  </code></pre>
  
  <p>And my view looks like this (unimportant stuff left out):</p>
  
  <pre><code>&lt;input name="@(cr.Category.naam)-name-@i" value="" /&gt;
  &lt;input name="@(cr.Category.naam)-dateOfBirth-@i" value="" /&gt;
  &lt;input name="@(cr.Category.naam)-passportno-@i" value="" /&gt;
  
  @if (ViewBag.ErrorMessage != null)
  {
       &lt;p class="error"&gt;@ViewBag.ErrorMessage&lt;/p&gt;
  }
  </code></pre>
  
  <p>So what happens when you forget to fill in a required field? It returns the view and displays the error message, BUT all the fields that were already filled in are empty again... So what I want to do is use the FormCollection to fill them back in after it returns the view. I put the FormCollection in the ViewBag like this:</p>
  
  <pre><code>ViewBag.ErrorMessage = "Please fill in all required fields at " + cr.Category.naam;
  ViewBag.FormCollection = formCollection;
  return View("BulletinForm3");
  </code></pre>
  
  <p>And changed this in the view:</p>
  
  <pre><code>&lt;input name="@(cr.Category.naam)-name-@i" value="@if (ViewBag.FormCollection != null) { ((FormCollection)ViewBag.FormCollection).Get(cr.Category.naam + "-name-" + i);}" /&gt;
  </code></pre>
  
  <p>However, it doesn't fill in the input fields with the values of the FormCollection... The FormCollection isn't empty (tested it) and the names are the same (the counter (i) doesn't change or something).
  What am I doing wrong?</p>
  
  <p>Thanks in advance</p>