开发者

MVC Validating Strongly Typed Views

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<A开发者_JAVA技巧dminGroup> 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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜