如何在ASP客户端触发自定义验证器.净MVC

本文关键字:验证 自定义 MVC ASP 客户端 | 更新日期: 2023-09-27 18:15:13

这是我的自定义验证器类:

public class PriceAttribute : ValidationAttribute {
    public double MinPrice { get; set; }
    public override bool IsValid(object value) {
        if (value == null) {
            return true;
        }
        var price = (double)value;
        if (price < MinPrice) {
            return false;
        }
        double cents = price - Math.Truncate(price);
        if (cents < 0.99 || cents >= 0.995) {
            return false;
        }
        return true;
    }
}

And my Model:

public class MyModel {
    public long Id { get; set; }
    [Price(MinPrice = 1.2, ErrorMessage = "hmm not good value")]
    public double Price { get; set; }
}

但是这个验证器在回发时触发。我如何实现它来像[Required]验证器一样触发客户端。是否有任何jQuery参考添加到视图页面?或者我需要一个自定义脚本来处理它?

如何在ASP客户端触发自定义验证器.净MVC

本文详细描述了为了获得客户端验证必须执行的步骤。

但是这个验证器触发回发

请注意MVC中没有回发,只是普通的HTTP post。

您最好使用jQuery验证引擎