Data annotations with decimal not working

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 correct method

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

      

it doesn't work either. By "not working" I mean it doesn't confirm. Why did 0 work but not some other number? How can we make this code?

+3


source to share


1 answer


I don't know what is your purpose for checking decimal places, however, maybe this trick works for you:

As you need 2 precision numbers, there is a DataType, 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 ...

0


source







All Articles