views:

26

answers:

1

I have following ViewModel:

    public IEnumerable<SelectListItem> SelectAdminsInGroup { get; set; }
    public IEnumerable<SelectListItem> SelectAdminsNotInGroup { get; set; }
    public model.Admin Admin { get; set; }

Admin class:

[PropertiesMustMatchAttribute("Password","ConfirmPassword")] public class Admin { public Admin() { this.PasswordDate = DateTime.Now; this.Username = string.Empty; }

    public virtual int AdminId { get; set; }

    [Required(ErrorMessage = "Field 'Username' is required"), StringLength(20, ErrorMessage = "Field 'Username' must be less than 20 characters long")]
    public virtual string Username { get; set; }
    public virtual string Oldusername { get; set; }

    [ValidatePasswordLength()]
    public virtual string Password { get; set; }
    public virtual string ConfirmPassword { get; set; }


    public virtual ICollection<AdminGroup> AdminGroup { get; set; }
}

Now for some reason PropertiesMustMatch never displays the error message in the validation summary. i also noticed that one of the ModelState keys contains "Admin" for "PropertiesMustMatchAttribute" and it should be blank since this would be the class/summary error message. How would i validate my custom model? thanks

+1  A: 

Type-level validators (e.g. PropertiesMustMatchAttribute) run only if all of the property-level validators succeeded. If the property-level validator fails, the type-level validator will not be run. Are you failing the "ValidatePasswordLength" check?

Jay
It doesn't fail, but it's not displayed. When I debug I see that it DOES get inserted into ModelState error list but because im using ModelView (which contains Admin class and otheres) the key for that error is not (blank) "" but has "Admin" in it therefore it's not being displayed in the error summary.
Shane Km