title: .input-validation-error to a textbox when the form is redisplayed for failed value
body: <p>I've asked a <a href="https://stackoverflow.com/questions/325515/mvc-asp-net-best-way-to-get-52-textbox-key-values-from-view-to-controller">question</a> to know why, in my application, textboxes are being highlighted (i.e. red border and pink-shaded backgroung are applied to the textbox) when I use modelbinding to validate the model (<strong>TryUpdateModel()</strong>) but not when I validate manually (<strong>ModelState.AddModelError</strong>). It has been 2 days now without any answer. I've tried every thing myself without succes. So, I decide to ask the question differently.</p>
  
  <p>The way I understand IT, here's how ModelBinding treats a request.</p>
  
  <ol>
  <li>ModelBinding get incoming values from httpcontext</li>
  <li>It instantiate an object of that model</li>
  <li>Tries to parse those values to the object</li>
  <li>If there's someting wrong with a property, it uses ModelState.AddModelError to mark properties that has something wrong with them.</li>
  <li>Re-displays the view</li>
  </ol>
  
  <p>Here's my question When the form is re-displayed: </p>
  
  <p><strong>What's being done for the textboxes whose values are not valid to get highlighted?</strong></p>
  
  <p>I know that there's few classes in Site.css, such as <strong>.input-validation-error</strong> and <strong>.field-validation-error</strong> that get applied to the textbox. Maybe ModelBinding uses internally a command such as AddCss("#MyTextBox", ".input-validation-error").</p>
  
  <p>If I know how it works, I can (maybe) aplly it manually and solve my problem.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As requested by @Ian Galloway, here's the code</p>
  
  <pre><code>public class RegistrationController : Controller
  {
    public FormViewModel formViewModel;
    private RegistrationService _registrationService = new RegistrationService();
    private SaveService _saveService = new SaveService();
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var serialized = Request.Form["formViewModel"];
          if (serialized != null)
          {
              formViewModel = (FormViewModel)new MvcSerializer()
                              .Deserialize(serialized);
              TryUpdateModel(formViewModel);
          }
          else
              formViewModel = (FormViewModel)TempData["formViewModel"] 
                              ?? new   FormViewModel();
      }
    protected override void OnResultExecuted(ResultExecutedContext filterContext)
      {
          if (filterContext.Result is RedirectToRouteResult)
              TempData["formViewModel"] = formViewModel;
      }
      public ActionResult SetUpOrganization(string cancel, string nextButton)
      {
          if ((nextButton != null) &amp;&amp; ModelState.IsValid)
          {
              if (formViewModel.navigationData.IsAReview)
                  return RedirectToAction("RequestPreview");
              return RedirectToAction("ChooseTypeOrganization");
          }
          ViewData["Cities"] = _registrationService.Get_Cities();
          formViewModel.navigationData.NextAction = "SetUpOrganization";
          return View(formViewModel);
      }
    public ActionResult ChooseTypeOrganization(string backButton, string nextButton)
      {
          if (backButton != null)
          {
              return RedirectToAction("SetUpOrganization");
          }
          if (nextButton != null)
          {
              if (formViewModel.navigationData.IsAReview)
                  return RedirectToAction("RequestPreview");
              return RedirectToAction("DocumentsPresented");
          }
          ViewData["TypeOrganization"] = _registrationService.Get_AllTypeOrganization();
          formViewModel.navigationData.NextAction = "ChooseTypeOrganization";
          return View(formViewModel);
      }
      public ActionResult DocumentsPresented(string backButton, string nextButton)
      {
          if (backButton != null)
          {
              return RedirectToAction("ChooseTypeOrganization");
          }
          if (nextButton != null)
          {
              //Validation
              if (string.IsNullOrEmpty(formViewModel.registrationData.DocumentPresente))
              {
                  ModelState.AddModelError("DocumentPresente", "Veuillez préciser votre
                    autorisation");
                  return View(formViewModel);
              }
              //Navigation
              if (formViewModel.navigationData.IsAReview)
                  return RedirectToAction("RequestPreview");
              return RedirectToAction("PeopleRecommended");
          }
          formViewModel.navigationData.NextAction = "DocumentsPresented";
          return View(formViewModel);
      }
    public ActionResult PeopleRecommended(string backButton, string nextButton, string deleteButton,
                                            string deletePerson, string addPerson)
      {
          if (backButton != null)
          {
              return RedirectToAction("DocumentsPresented");
          }
          if (nextButton != null)
          {
              ModelState.Clear();
              if (formViewModel.registrationData.PeopleRecommended.Count == 0)
                  ModelState.AddModelError("", "Il faut absolument designer un responsable pour la requête");
              //
              if (ModelState.IsValid)
              {
                  if (formViewModel.navigationData.IsAReview)
                      return RedirectToAction("RequestPreview");
                  return RedirectToAction("StateObjectifs");
              }
              else
              {
                  return View(formViewModel);
              }
          }
          //
          if (addPerson != null)
          {
              if (ModelState.IsValid)
              {
                  formViewModel.registrationData.PeopleRecommended.Add(
                      _registrationService.Translate_PersonToBeAdded_Into_Person(formViewModel.personToBeAdded)
                      );
                  formViewModel.personToBeAdded = null;
              }
              else
              {
                  formViewModel.navigationData.NextAction = "PeopleRecommended";
                  return View(formViewModel);
              }
          }
          if (deleteButton != null)
          {
              formViewModel.registrationData.PeopleRecommended.RemoveAt(int.Parse(deletePerson));
          }
          ViewData.ModelState.Clear();
          formViewModel.navigationData.NextAction = "PeopleRecommended";
          return View(formViewModel);
      }
      [ValidateInput(false)]
      public ActionResult StateObjectifs(string backButton, string nextButton)
      {
          if (backButton != null)
          {
              return RedirectToAction("PeopleRecommended");
          }
          if (nextButton != null)
          {
              if (string.IsNullOrEmpty(formViewModel.registrationData.Objective) ||
                 string.IsNullOrEmpty(formViewModel.registrationData.RequestDetails))
              {
                   if (string.IsNullOrEmpty(formViewModel.registrationData.Objective))
                       ModelState.AddModelError("Objective", "Vous devez préciser l'objectif de votre requête");
  
                   if (string.IsNullOrEmpty(formViewModel.registrationData.RequestDetails))
                       ModelState.AddModelError("RequestDetails", "Vous devez préciser le contenu de votre requête");
                   return View(formViewModel);
              }
  
              if (formViewModel.navigationData.IsAReview)
                  return RedirectToAction("RequestPreview");
              return RedirectToAction("StateDeadLine");
          }
          return View(formViewModel);
      }
  
      public ActionResult StateDeadLine(string backButton, string nextButton)
      {
          if (backButton != null)
          {
              return RedirectToAction("StateObjectifs");
          }
          if (nextButton != null)
          {
              if (formViewModel.registrationData.ChooseDifferentDeadLine)
              {
                  if (formViewModel.registrationData.DifferentDeadline == null ||
                      string.IsNullOrEmpty(formViewModel.registrationData.ReasonsForDifferentDeadLine))
                  {
                      if (formViewModel.registrationData.DifferentDeadline == null)
                          ModelState.AddModelError("DifferentDeadline", "Clickez pour choisir une nouvelle date");
                      if (string.IsNullOrEmpty(formViewModel.registrationData.ReasonsForDifferentDeadLine))
                          ModelState.AddModelError("ReasonsForDifferentDeadLine", "Expliquez brievement pour quoi ce changement");
  
                      return View(formViewModel);
                  }
              }
              return RedirectToAction("RequestPreview");
          }
          formViewModel.navigationData.NextAction = "StateDeadLine";
          return View(formViewModel);
      }
    public ActionResult RequestPreview(string backButton, string nextButton, string reviewInput, string save)
      {
          if (backButton != null)
          {
              return RedirectToAction("StateDeadLine");
          }
          if (nextButton != null)
          {
              _saveService.Save_FormViewModel(formViewModel);   
              return RedirectToAction("Index", "Home");
          }
          if (reviewInput != null)
          {
              formViewModel.navigationData.IsAReview = true;
              return RedirectToAction(RedirectHelpers.RedirectReviewAction(formViewModel.navigationData, reviewInput));
          }
          ViewData["TypeOrganization_Summary"] = _registrationService.Get_TypeOrganization_Summary(
                                                                  formViewModel.registrationData.TypeOrganizationID                                                                   );
          if (save != null)
          {
              _saveService.Save_FormViewModel(formViewModel);
              return RedirectToAction("Index", "Home");
          }
  
          formViewModel.navigationData.NextAction = "RequestPreview";
          return View(formViewModel);
      }
  }
  </code></pre>
  
  <p><strong>EDIT number 2</strong></p>
  
  <p>I have choosed one of the view (as all of them experience the same issue). The view is called <strong>StateObjectifs.aspx</strong></p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;!-- Script Begin --&gt;
  &lt;script src="../../Scripts/tiny_mce/tiny_mce.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type = "text/javascript"&gt;
      tinyMCE.init({
          mode: "textareas",
          theme: "advanced",
  
          theme_advanced_toolbar_location: "top",
          theme_advanced_toolbar_align: "left",
          theme_advanced_statusbar_location: "bottom",
          theme_advanced_resizing: true
      });
  &lt;/script&gt;
   &lt;!-- End Script --&gt;
  &lt;fieldset id = "StateObjectifs"&gt;
      &lt;legend&gt;Etape 5:&lt;/legend&gt;
      &lt;div id = "left-pane" style = "width:700px"&gt;
      &lt;%using (Html.BeginForm("StateObjectifs", "Registration"))
    { %&gt;
     &lt;%: Html.ValidationSummary() %&gt;
    &lt;% = Html.Serialize("formViewModel", Model) %&gt; 
    &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;&lt;% = Html.LabelFor(x=&gt;x.registrationData.Objective) %&gt;&lt;/th&gt;
          &lt;td&gt;&lt;% = Html.TextBoxFor(x=&gt;x.registrationData.Objective) %&gt;
              &lt;%: Html.ValidationMessageFor(x =&gt;x.registrationData.Objective, "*")%&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;th colspan = "2"&gt;&lt;% = Html.LabelFor(x =&gt;x.registrationData.RequestDetails) %&gt;&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td colspan = "2"&gt;
              &lt;% = Html.TextAreaFor(x =&gt;x.registrationData.RequestDetails, 10, 75, null) %&gt;
  
          &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/div&gt;
    &lt;div id = "right-pane"&gt;
          &lt;ul&gt;
              &lt;li&gt;Les cases pour lesquelles le titre se termine par (*) sont obligatoires&lt;/li&gt;
              &lt;li&gt;Pour mieux vous servir, veuillez décrire clairement de manière concise
                  votre requête. Par exemple: &lt;i&gt;Les données de l'USAID sur le secteur santé pour l'année 2009&lt;/i&gt;&lt;/li&gt;
          &lt;/ul&gt;  
        &lt;/div&gt;
        &lt;div class = "clear"&gt;&lt;/div&gt;
  
    &lt;fieldset&gt;
              &lt;% Html.RenderPartial("NavigationView", Model.navigationData); %&gt;
    &lt;/fieldset&gt;
  &lt;/fieldset&gt;
  &lt;%} %&gt;
  </code></pre>
  
  <p></p>
  
  <p>Thanks for helping.</p>