Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal value is not valid for price. MVC3

Product.cs

...
    [Required(ErrorMessage="Price is required")]
    [Range(0.01, 100000.00,
        ErrorMessage="Price must be between 0.01 and 100000.00")]
    public decimal Price { get; set; }
...

When I enter '89.48', form is giving 'The value '89.48' is not valid for Price'. I think this is becuase of default language of my PC. It is not English. It is Russian. enter image description here

I tried to solve this issue by haacked.com instructions:

  1. I have created Model class DecimalModelBinder and copied code from haacked.com into class
  2. Updated Global.asax with

    ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
    

    No effect. Then I tried to fix it by client-side validation 1.Added JavaScript file called "jQueryFixes.js" with code

$.validator.methods.range = function (value, element, param) {
    var globalizedValue = value.replace(",", ".");
    return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}

$.validator.methods.number = function (value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}

This code did not solve this issue. Can you suggest what I am doing wrong here?

like image 475
Joe Rakhimov Avatar asked Mar 02 '14 09:03

Joe Rakhimov


1 Answers

I set in web.config

<system.web>
    <globalization uiCulture="en-US" culture="en-US"/>
<system.web>

this solution worked for me, i was getting the same error

like image 73
Mehdi Benkirane Avatar answered Sep 30 '22 15:09

Mehdi Benkirane