title: MVC Model Validation From Database
body: <p>I have a very simple Model, that needs to get validated from Database</p>
  
  <pre><code>public class UserAddress
  {
      public string CityCode {get;set;}
  }
  </code></pre>
  
  <p><code>CityCode</code> can have values that are only available in my database table.</p>
  
  <p>I know i can do something like.</p>
  
  <pre><code>[HttpPost]
  public ActionResult Address(UserAddress model)
  {
      var connection = ; // create connection
      var cityRepository = new CityRepository(connection);
  
      if (!cityRepository.IsValidCityCode(model.CityCode))
      {
          // Added Model error
      }
  }
  </code></pre>
  
  <p>This seems very <code>WET</code> as I have to use this model at lot of placed and adding the same logic each place seems like i am not using MVC Architecture properly.</p>
  
  <p><em>So, what is the best pattern to validate model from Database ?</em> </p>
  
  <p><strong>NOTE:</strong>
  Most of the validation are single field lookup from Database, other validation may include combination of field. But right now I am happy with single field lookup validation, as long as it is <code>DRY</code> and is not using too much reflection it is acceptable.</p>
  
  <p><strong>NO CLIENT SIDE VALIDATION :</strong> 
  <em>To anyone who is answering in terms of client side validation, I do not need any such validation, most of my validation are server sided, and I need the same, please do not answer with Client Side Validation methods.</em></p>
  
  <hr>
  
  <p>P.S. If any one can give me hint how to do a attribute based validation from Database, will be extremely greatful.</p>