title: Best way to trim the strings inside my asp.net mvc
body: <p>I have a string field name consoleServerPort, and I want to trim all the white spaces inside it before saving it. I use to do so inside my controller class, as follow:-</p>
  
  <pre><code>public ActionResult Edit(FirewallJoin fj, FormCollection formValues)
          {
  fj.ConsoleServerPort = !String.IsNullOrEmpty(fj.ConsoleServerPort) ? fj. ConsoleServerPort.Trim() : "";
  </code></pre>
  
  <p>But I have to repeat this step on every action method. So I found another way of doing so once at the model level inside an Ivalidatable method as follow:-</p>
  
  <pre><code>public partial class TMSFirewall : IValidatableObject
      {
  public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
          {
  
              if(!String.IsNullOrEmpty(ConsoleServerPort)){
                  ConsoleServerPort = ConsoleServerPort.Trim();
              }
  </code></pre>
  
  <p>So is my second approach sound valid? or it is better to use the first approach ?
  Thanks </p>
  
  <p><strong>Edit</strong>
  I am using entity Framework and i did the following
  i tried the following inside my model class i added the following :-</p>
  
  <pre><code>[MetadataType(typeof(TMSSwitchPort_Validation))]
      [Bind(Include = "PortNumber,SwitchID,TechnologyID")]
      public partial class TMSSwitchPort //: IValidatableObject
      {
  
      }
  </code></pre>
  
  <p>then inside the MetadataType class i added the following :-</p>
  
  <pre><code>public class TMSSwitchPort_Validation
      {
          private string consoleServerPort;
  
          [Required]
          [StringLength(10)]
          [Display(Name="Port Number1111")]
          public String PortNumber
          {
              get { return this.consoleServerPort; }
              set { this.consoleServerPort = value.Trim(); }
          }
  
  
      }
  </code></pre>
  
  <p>but the ConsoleServerPort will not be trimmed? can you advice what might be the problem ?</p>
  
  <p>Thanks</p>