title: Trim every input field except those with NoTrim attribute
body: <p>I am working on an ASP.NET MVC 2 application that I didn't create. All input fields in the application are trimmed during model binding. However, I want to have a NoTrim attribute that prevents certain fields from being trimmed.</p>
  
  <p>For instance, I have the following state drop-down field:</p>
  
  <pre><code>&lt;select name="State"&gt;
      &lt;option value=""&gt;Select one...&lt;/option&gt;
      &lt;option value="  "&gt;International&lt;/option&gt;
      &lt;option value="AA"&gt;Armed Forces Central/SA&lt;/option&gt;
      &lt;option value="AE"&gt;Armed Forces Europe&lt;/option&gt;
      &lt;option value="AK"&gt;Alaska&lt;/option&gt;
      &lt;option value="AL"&gt;Alabama&lt;/option&gt;
      ...
  </code></pre>
  
  <p>The problem is that when a user chooses "International", I get a validation error because the two spaces are trimmed and State is a required field.</p>
  
  <p>Here's what I'd like to be able to do:</p>
  
  <pre><code>    [Required( ErrorMessage = "State is required" )]
      [NoTrim]
      public string State { get; set; }
  </code></pre>
  
  <p>Here's what I've got for the attribute so far:</p>
  
  <pre><code>[AttributeUsage( AttributeTargets.Property, AllowMultiple = false )]
  public sealed class NoTrimAttribute : Attribute
  {
  }
  </code></pre>
  
  <p>There is a custom model binder that gets set up in Application_Start:</p>
  
  <pre><code>protected void Application_Start()
  {
      ModelBinders.Binders.DefaultBinder = new MyModelBinder();
      ...
  </code></pre>
  
  <p>Here is the part of the model binder that does the trimming:</p>
  
  <pre><code>protected override void SetProperty( ControllerContext controllerContext,
                                       ModelBindingContext bindingContext,
                                       PropertyDescriptor propertyDescriptor,
                                       object value )
  {
      if (propertyDescriptor.PropertyType == typeof( String ) &amp;&amp; !propertyDescriptor.Attributes.OfType&lt;NoTrimAttribute&gt;().Any() )
      {
          var stringValue = (string)value;
  
          if (!string.IsNullOrEmpty( stringValue ))
          {
              value = stringValue.Trim();
          }
      }
  
      base.SetProperty( controllerContext, bindingContext, propertyDescriptor, value );
  }
  </code></pre>