title: Model binding and display trimmed string property
body: <p>My strongly typed View inherits from a "Person" object that is created with Linq to SQL. In my "Edit" View, i have to display of course old values:</p>
  
  <pre><code>&lt;%= Html.TextBox("FirstName") %&gt;
  </code></pre>
  
  <p>"FirstName" is NCHAR, so it need to be trimmed. So i ended up with:</p>
  
  <pre><code>&lt;%= Html.TextBox("FirstName", Model.FirstName.Trim()) %&gt;
  </code></pre>
  
  <p>and this works. But when form is submitted (after POST) and some errors occur, i need to show it again:</p>
  
  <pre><code>[AcceptVerbsAttribute(HttpVerbs.Post), Authorize(Roles = "office"), HandleError]
  public ActionResult Edit(Models.Person person)
  {
      if (!(_personService.ValidatePerson(person))) // Persona non valida
      { return View(person); }
  }
  </code></pre>
  
  <p>If for some reason the user left the textbox "FirstName" <strong>blank</strong>, the resulting property Person.FirstName become <strong>null</strong> and Model.FirstName.Trim() throws an Exception (Object reference not set to an instance of an object).</p>
  
  <p><em>Any way to modify the bind and have all string trimmed by default? Or any ideas to how fix this</em>?</p>
  
  <p><strong>Update:</strong> seems confirmed to be a MVC 2 behaviour.. still looking for a good way to handle this. Actually using an extension method:</p>
  
  <pre><code>public static string TrimOrDefault(this string value)
  {
      return value != null ? value.Trim() : string.Empty;
  }
  </code></pre>