Min/Max-value validators in asp.net mvc

前端 未结 4 1837
时光说笑
时光说笑 2020-12-04 14:56

Validation using attributes in asp.net mvc is really nice. I have been using the [Range(min, max)] validator this far for checking values, like e.g.:



        
4条回答
  •  鱼传尺愫
    2020-12-04 15:36

    Here is how I would write a validator for MaxValue

    public class MaxValueAttribute : ValidationAttribute
        {
            private readonly int _maxValue;
    
            public MaxValueAttribute(int maxValue)
            {
                _maxValue = maxValue;
            }
    
            public override bool IsValid(object value)
            {
                return (int) value <= _maxValue;
            }
        }
    

    The MinValue Attribute should be fairly the same

提交回复
热议问题