代码之家  ›  专栏  ›  技术社区  ›  mathieu

ASP.NET MVC:DataAnnotation验证执行顺序

  •  3
  • mathieu  · 技术社区  · 14 年前

    我在理解数据注释验证之后的验证逻辑时遇到一些问题:

    使用以下型号:

    [AlwaysInvalid]
    public class TestModel
    {
        [Required]
        public string Test { get; set; }
    }
    
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public class AlwaysInvalidAttribute : ValidationAttribute
    {
        private readonly object typeId = new object();
    
        public AlwaysInvalidAttribute() : base("Fail !") {}
    
        public override object TypeId { get { return this.typeId; } }
    
        public override bool IsValid(object value)
        {
            return false;
        }
    }
    

    只有在必需的属性有效时,才会显示AlwaysInValidation错误消息:我不能同时获取这两条消息。有人知道为什么吗?我认为这是DefaultModelBinder的问题,但仍然没有找到在哪里,或者为什么。

    1 回复  |  直到 14 年前
        1
  •  4
  •   marcind    14 年前

    ModelValidator