When I post a 'Model' object (as generated by LinqToSQL) to a controller, I can query 'ModelState.IsValid', and if there are validation attributes on any of the properties and the value doesn't validate, it will be set to 'false'.
However, ModelState.IsValid seems to always return 'true' if I'm posting a custom object of my own class, even if the properties on that class have validation attributes and are given incorrect values.
Why does this only work with DataContext model objects? What is it about these objects that makes them work with ModelState.IsValid, while normal classes don't?
How can I make it work with normal classes?
Controller code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogIn(MyProject.Website.ViewModels.Shared.LogIn model)
{
if (ModelState.IsValid)
return View(model);
// ... code to log in the user
}
ViewModel code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MyProject.Website.Validators;
using System.ComponentModel;
public class LogIn
{
public LogInModes LogInMode { get; set; }
[Required]
[EmailAddress]
public string EmailAddress { get; set; }
public string Password { get; set; }
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
}