Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从MVC范围属性c中排除少数数字#_C#_Asp.net Mvc - Fatal编程技术网

C# 如何从MVC范围属性c中排除少数数字#

C# 如何从MVC范围属性c中排除少数数字#,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个带有范围(0999)的范围属性。我想从范围属性中排除1,2。我该怎么做 我需要从这个范围中排除1,2 Range(0, 999, ErrorMessageResourceName = "NumberRange", ErrorMessageResourceType = typeof(Resource)) 您始终可以创建自定义验证属性,并在模型中使用该属性 public class CustomRangeAttribute: ValidationAttribute, I

我有一个带有
范围(0999)
的范围属性。我想从
范围
属性中排除1,2。我该怎么做

我需要从这个范围中排除1,2

Range(0, 999, ErrorMessageResourceName = "NumberRange", ErrorMessageResourceType = typeof(Resource))

您始终可以创建自定义验证属性,并在模型中使用该属性

public class CustomRangeAttribute: ValidationAttribute, IClientValidatable
{
    private double _MinValue, _MaxValue

    public CustomRangeAttribute(double min, double max, Func<string> errorMessageAccessor) : base(errorMessageAccessor)
    {
        _MinValue = min;
        _MaxValue = max;
    }

    public override bool IsValid(object value)
    {
        double val = (double)value;
        return val >= _MinValue && val <= _MaxValue;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessage, _MinValue, _MaxValue);
    }
    
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage,
            ValidationType = "futuredate"
        };
    }
}
解决了我的问题

[RegularExpression("^[0 3-9]*$", ErrorMessageResourceName = "NumbersAllowed", ErrorMessageResourceType = typeof(Resource))]      

   [Range(0, 999, ErrorMessageResourceName = "NumberRange", ErrorMessageResourceType = typeof(Resource))]
可枚举范围(0,1000)。其中(x=>(x!=1)和&(x!=2))
[RegularExpression("^[0 3-9]*$", ErrorMessageResourceName = "NumbersAllowed", ErrorMessageResourceType = typeof(Resource))]      

   [Range(0, 999, ErrorMessageResourceName = "NumberRange", ErrorMessageResourceType = typeof(Resource))]