title: ModelState.IsValid vs IValidateableObject in MVC3
body: <p>so according to <a href="http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx" rel="noreferrer">Gu</a> <code>IValidatableObject.Validate()</code> should get called when a controller validates it's model (i.e. before <code>ModelState.IsValid</code>) however simply making the model implement <code>IValidatableObject</code> doesn't seem to work, because <code>Validate(..)</code> doesn't get called.</p>
  
  <p>Anyone know if there is something else I have to wire up to get this to work?</p>
  
  <p>EDIT:</p>
  
  <p>Here is the code as requested.</p>
  
  <pre><code>public class LoginModel : IValidatableObject
  {
      [Required]
      [Description("Email Address")]
      public string Email { get; set; }
  
      [Required]
      [Description("Password")]
      [DataType(DataType.Password)]
      public string Password { get; set; }
  
      [DisplayName("Remember Me")]
      public bool RememberMe { get; set; }
  
      public int UserPk { get; set; }
  
      public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
      {
          var result = DataContext.Fetch( db =&gt; {
  
              var user = db.Users.FirstOrDefault(u =&gt; u.Email == Email);
  
              if (user == null) return new ValidationResult("That email address doesn't exist."); 
              if (user.Password != User.CreateHash(Password, user.Salt)) return new ValidationResult("The password supplied is incorrect.");
  
              UserPk = user.UserPk;
              return null;
          });
  
          return new List&lt;ValidationResult&gt;(){ result };
      }
  }
  </code></pre>
  
  <p>The action. ( I don't do anything special in the Controller...)</p>
  
  <pre><code>[HttpPost]
  public ActionResult Login(LoginModel model)
  {
      if (ModelState.IsValid)
      {
          FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
          return Redirect(Request.UrlReferrer.AbsolutePath);
      }
  
      if (ControllerContext.IsChildAction || Request.IsAjaxRequest())
          return View("LoginForm", model);
  
      return View(model);
  }
  </code></pre>
  
  <p>I set a break point on the first line of <code>LoginModel.Validate()</code> and it doesn't seem to get hit.</p>