title: Custom DateTime model binder in Asp.net MVC
body: <p>I would like to write my own model binder for <code>DateTime</code> type. First of all I'd like to write a new attribute that I can attach to my model property like:</p>
  
  <pre><code>[DateTimeFormat("d.M.yyyy")]
  public DateTime Birth { get; set,}
  </code></pre>
  
  <p>This is the easy part. But the binder part is a bit more difficult. I would like to add a new model binder for type <code>DateTime</code>. I can either</p>
  
  <ul>
  <li>implement <code>IModelBinder</code> interface and write my own <code>BindModel()</code> method</li>
  <li>inherit from <code>DefaultModelBinder</code> and override <code>BindModel()</code> method</li>
  </ul>
  
  <p>My model has a property as seen above (<code>Birth</code>). So when the model tries to bind request data to this property, my model binder's <code>BindModel(controllerContext, bindingContext)</code> gets invoked. Everything ok, but. <strong>How do I get property attributes from controller/bindingContext, to parse my date correctly</strong>? How can I get to the <code>PropertyDesciptor</code> of property <code>Birth</code>?</p>
  
  <h2>Edit</h2>
  
  <p>Because of separation of concerns my model class is defined in an assembly that doesn't (and shouldn't) reference System.Web.MVC assembly. Setting custom binding (similar to <a href="http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx" rel="noreferrer">Scott Hanselman's example</a>) attributes is a no-go here.</p>