简体   繁体   中英

ErrorMessage with Range using Data annotations

I have this:

    [Range(1, 1000, ErrorMessage = "Enter a value between 1 and 1000")]
    public object ObjectLimit { get; set; }

Works great, but it doesnt show the error message, it shows some standard "The value '554g' is not valid for the ObjectLimit field."

How do I fix that?

Your setting a range of valid integers , 554g is not an integer.

Looks like you need a RegulagExpression attribute with the correct expression to match 554g as a correct value.

You have to add a regular expression in this case, like:

 [RegularExpression(@"^[1-1000]{1,4}$", ErrorMessage = "RangeAllowed")]
 public object ObjectLimit { get; set; }

This will catch if you provide non integer values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM