title: Redisplay entered details if error on form ASP.NET MVC
body: <p>I have been working on this problem for 2 days now and it's an easy problem and I just can't see the error in the code, even after comparing it with other projects where this works.</p>
  
  <p>Could you help out?</p>
  
  <p>I'm working on the account section of my website using ASP.NET Membership and the account controller class that is generated with ASP.NET MVC.</p>
  
  <p>However when registering and the user leaves some fields blank the entered text dosn't show up when the page is redisplayed.</p>
  
  <p>Here is the register post action</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Register(FormCollection formValues)
      {
  
          ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
  
          if (ValidateRegistration(formValues))
          {
  
              // Attempt to register the user
              MembershipCreateStatus createStatus = MembershipService.CreateUser(formValues["username"], formValues["password"], formValues["email"]);
  
              if (createStatus == MembershipCreateStatus.Success)
              {
                  FormsAuth.SignIn(formValues["username"], false /* createPersistentCookie */);
  
                  return RedirectToAction("CreateCustomer", "Account");
              }
              else
              {
                  ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View();
      }
  </code></pre>
  
  <p>And the ValidateRegistration function</p>
  
  <pre><code> private bool ValidateRegistration(FormCollection formValues)
      {
          if (String.IsNullOrEmpty(formValues["userName"]))
          {
              ModelState.AddModelError("username", "You must specify a username.");
          }
          if (String.IsNullOrEmpty(formValues["email"]))
          {
              ModelState.AddModelError("email", "You must specify an email address.");
  
          }
          if (formValues["password"] == null || formValues["password"].Length &lt; MembershipService.MinPasswordLength)
          {
              ModelState.AddModelError("password",
                  String.Format(CultureInfo.CurrentCulture,
                       "You must specify a password of {0} or more characters.",
                       MembershipService.MinPasswordLength));
  
          }
          if (!String.Equals(formValues["password"], formValues["confirmPassword"], StringComparison.Ordinal))
          {
              ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
          }
          if (!String.Equals(formValues["email"], formValues["confirmEmail"], StringComparison.Ordinal))
          {
              ModelState.AddModelError("confirmEmail", "The email and confirmation email addresses do not match.");
          }
          return ModelState.IsValid;
      }
  </code></pre>
  
  <p>And just in case here is the Register view</p>
  
  " %>
  
  <p>
      Register
  </p>
  
  <p>
     <h2>Create a New Account</h2>
      <p>
          Use the form below to create a new account. 
      </p>
      <p>
          Passwords are required to be a minimum of &lt;%=Html.Encode(ViewData["PasswordLength"])%> characters in length.
      </p>
      <br />
      &lt;%= Html.ValidationSummary() %>
  <br />
      &lt;% using (Html.BeginForm()) { %>
          
              
                  
                  Username:
                   &lt;%= Html.TextBox("username")%> </p>
  
  <pre><code>                &lt;%= Html.ValidationMessage("username") %&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
              &lt;td&gt;Email:&lt;/td&gt;
              &lt;td&gt;&lt;%= Html.TextBox("email") %&gt;
                  &lt;%= Html.ValidationMessage("email") %&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
              &lt;td&gt;Confirm Email:&lt;/td&gt;
              &lt;td&gt;
                  &lt;%= Html.TextBox("confirmEmail") %&gt;
                  &lt;%= Html.ValidationMessage("confirmEmail") %&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
              &lt;td&gt;Password:&lt;/td&gt;
              &lt;td&gt;
                  &lt;%= Html.Password("password") %&gt;
                  &lt;%= Html.ValidationMessage("password") %&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
              &lt;td&gt;Confirm password:&lt;/td&gt;
              &lt;td&gt;
                  &lt;%= Html.Password("confirmPassword") %&gt;
                  &lt;%= Html.ValidationMessage("confirmPassword") %&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
  
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;
                  &lt;input type="submit" value="Register" /&gt;
              &lt;/td&gt;
              &lt;/table&gt;
      &lt;/div&gt;
  
  
  &lt;% } %&gt;
  </code></pre>
  
  <p> </p>
  
  <p></p>