验证上下文始终为 NULL?

发布于 2024-10-25 17:05:36 字数 655 浏览 3 评论 0原文

我有这样的自定义验证属性:

    public class MyCustomAttribute : ValidationAttribute {
    protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
        if ((int)value == 100) {
            // do some checking to validate & return ValidationResult accordingly

        } else return ValidationResult.Success;
    }
}

在这样的使用中:

    [DisplayName("My Custom Property")]
    [MyCustom(ErrorMessage = "ERROR!!!")]
    public int? MyCustomProperty { get; set; }

我的问题是:为什么在 MyCustomAttribute 中的 IsValid 方法中,validationContext 始终为 NULL?我需要设置什么特殊的东西才能使其不为空吗?

I have custom validation attribute such as this:

    public class MyCustomAttribute : ValidationAttribute {
    protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
        if ((int)value == 100) {
            // do some checking to validate & return ValidationResult accordingly

        } else return ValidationResult.Success;
    }
}

In usage like this:

    [DisplayName("My Custom Property")]
    [MyCustom(ErrorMessage = "ERROR!!!")]
    public int? MyCustomProperty { get; set; }

My question is: why is it that inside MyCustomAttribute, within the IsValid method, validationContext is always NULL? Is there anything special I need to set to get it not to be NULL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-11-01 17:05:37

如果你用来

ValidationResult IsValid(object value, ValidationContext validationContext)

检查数据是否有效,你必须使用

v.GetValidationResult(propertyValue,new ValidationContext(this))!= ValidationResult.Success

而不是

 v.IsValid(propertyValue)

if you use

ValidationResult IsValid(object value, ValidationContext validationContext)

to check if data is valid you have to use

v.GetValidationResult(propertyValue,new ValidationContext(this))!= ValidationResult.Success

instead of

 v.IsValid(propertyValue)
我不会写诗 2024-11-01 17:05:37

您必须覆盖 RequiresValidationContext


public override bool RequiresValidationContext =>; true;

它会起作用

you must override RequiresValidationContext


public override bool RequiresValidationContext => true;

it will work

流殇 2024-11-01 17:05:37

我遇到了这个问题,事实证明这与我(不小心)在自定义 ValidationAttribute 中调用 base.IsValid(value, validationContext) 有关。例如,

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
{
    if ((int)value == 100 
    {
        return base.IsValid(value, validationContext);
    } 
    else return ValidationResult.Success;
}

value == 100 为 true 且调用进入 base.IsValid(value,validationContext) 的场景中,这会导致额外 调用我的自定义验证,在第二次调用中 validationContext 为 null。

我设法通过简单地删除对 base.IsValid(value,validationContext) 的调用来解决这个问题,这对我来说很好(我从来没有打算进行这个调用 - 它是自动生成代码的剩余部分)当使用智能感知覆盖该方法时)。但是我想在某些情况下您确实想要/需要调用基本实现。我不知道在这些情况下您会采取什么措施来解决错误。

I had this issue and it turned out to be something to do with the fact I was (accidentally) calling base.IsValid(value, validationContext) within my custom ValidationAttribute. e.g.

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
{
    if ((int)value == 100 
    {
        return base.IsValid(value, validationContext);
    } 
    else return ValidationResult.Success;
}

In the scenario were value == 100 was true and the call went into base.IsValid(value, validationContext), this was causing an additional call to my custom validation, and in the second call validationContext was null.

I managed to fix this by simply removing the call to base.IsValid(value, validationContext), which was fine for me (I never intended to make this call - it was leftover from the auto-generate code when using intellisense to override the method). However I imagine there may be some cases where you do actually want/need to call the base implementation. I dont know what you would do to solve the error in these cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文