The range attribute allows you to specify that a specific field should only be able to accept a certain range of values.
By default the RangeAttribute will work with Int32 or Double values but it is possible to specify your own type and then define the max and min values as strings this then provides the ability to do things like date range checking.
For Int32 and Double the constructor looks like this:
Range(“Minimum Value”, “Maximum Value”, “Error Message”, “Error Resource”, “Error Resource Type”)
For the custom type the constructor looks like this:
Range(“Type”, “Minimum Value”, “Maximum Value”, “Error Message”, “Error Resource”, “Error Resource Type”)
We can see that Range shares similarities with the DataType attribute that I covered previously so if the field being validated fails validation you can either specify a string or message or use a resource for the error message to be displayed.
So to use it you simply decorate your meta class with the attribute on the field you want validated
[Range(1,100,ErrorMessage ="Value must be between 1 and 100")]
public object Quantity { get; set; }
Which then ends up being displayed on the site as:
No comments:
Post a Comment