MVC验证强类型视图

时间:2010-10-05 11:02:20

标签: asp.net asp.net-mvc asp.net-mvc-2

我有以下ViewModel:

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

管理员课程:

[PropertiesMustMatchAttribute( “密码”, “ConfirmPassword”)]  公共课管理员     {         公共管理员()         {             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; }
}

现在由于某种原因,PropertiesMustMatch从不在验证摘要中显示错误消息。我还注意到其中一个ModelState键包含“PropertiesMustMatchAttribute”的“Admin”,它应该是空白的,因为这将是类/摘要错误消息。 我如何验证我的自定义模型? 感谢

1 个答案:

答案 0 :(得分:2)

仅当所有属性级验证程序都成功时,才会运行类型级验证程序(例如,PropertiesMustMatchAttribute)。如果属性级验证器失败,则不会运行类型级验证器。您是否未通过“ValidatePasswordLength”检查?