DataAnnotations 中的自定义验证器属性异常
以前有人见过这个异常吗? Google 或 Bing 的结果绝对很少。
IsValid(object value) has not been implemented by this class.
The preferred entry point is GetValidationResult() and classes should override
IsValid(object value, ValidationContext context).
这是自定义验证器:
public class PriceAttribute : ValidationAttribute
{
public string Id { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//I think this definition for IsValid is in DataAnnotations 4.0
return base.IsValid(value, validationContext);
}
public override bool IsValid(object value)
{
//This I think is the older definition. Not sure why it expects this
return base.IsValid(value);
}
}
谢谢!
Has anyone seen this exception before? Google or Bing has absolutely very few results.
IsValid(object value) has not been implemented by this class.
The preferred entry point is GetValidationResult() and classes should override
IsValid(object value, ValidationContext context).
Here's the custom validator:
public class PriceAttribute : ValidationAttribute
{
public string Id { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//I think this definition for IsValid is in DataAnnotations 4.0
return base.IsValid(value, validationContext);
}
public override bool IsValid(object value)
{
//This I think is the older definition. Not sure why it expects this
return base.IsValid(value);
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上应该为这些方法之一提供实现,而不是调用
base.IsValid(value)
或base.IsValid(value,validationContext)
。如果可以在不检查上下文的其他值的情况下验证该值,则只需重写
IsValid(object value)
即可。You should actually provide an implementation for one of those methods instead of calling
base.IsValid(value)
orbase.IsValid(value, validationContext)
.If the value can be validated without examining other values of the context, you can just override
IsValid(object value)
.