Data Annotations with Decimal not working

Go To StackoverFlow.com

3

We have code like this:

    [DisplayName("Refresh Rate")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:G2}")]
    [Range(1.00, 150.00, ErrorMessage = "Must be between 1 and 150")]
    public virtual decimal RefreshRate { get; set; }

which doesn't work. If we change it to

[Range(0.00, 150.00, ....

it works. If we try to use the proper method of

[Range(typeof(Decimal), "1", "150", ....

it doesn't work either. By "doesn't work" I mean that it doesn't validate. Why would 0 work, but not any other number? How can we make this code work?

2012-04-04 04:11
by Matthew Talbert


0

I don't know what's your purpose to validating decimals, however maybe this trick works for you:

As you need 2 precision numbers, there is a DataType that maybe you can use it: Currency. So, maybe the following works for you:

[DisplayName("Refresh Rate")]
[Datatype(DataType.Currency)]
[Range(1, 150, ErrorMessage = "Must be between 1 and 150")]
public virtual decimal RefreshRate { get; set; }

Please tell me if it doesn't work...

2013-07-25 10:04
by Amin Saqi
Ads