Mahajan344 Mahajan344 - 4 years ago 109
C# Question

Range attribute issue in data annotation

I am working on Web APi where I have put Range attribute on double nullable field.Here is expression.

[Range(0, 6,ErrorMessage = "Value for {0} must be between {1} and {2}.")]
public Nullable<double> int_term { get; set; }

But when I test it only accepting one digit ie. 1 or 2 upto 6 etc.. if i put 12 then it through error.

"Value for int_term_depth_eng must be between 0 and 6."

What could be expression to allow upto 6 digits or +/- 5 digits

Answer Source

The Range attribute dictates the numerical range that can be allowed as input. Thus it only allows between 0 and 6.

If you want it to allow 6 digits then you need to make it this

[Range(-99999, 999999, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download