Mohammad Dayyan Mohammad Dayyan - 1 year ago 130
C# Question

Decimal symbol issue in MVC, C#?

I'm using MVC 5.0

I set the culture in config:

<globalization uiCulture="fa-IR" culture="fa-IR" />

I have a model as the following:

public class MyModel
[Range(0, 100)]
public double NbatPersent{ get; set; }

MVC shows the
value in View like
and when I wanna submit form the form validator alert me
The field NbatPersent must be a number.
. It can't convert

It will be OK if I enter 22.5 but if the property has a value it convert

How can I convert all numeric properties' culture to
to show value like
not like


I'm using
to show the decimal property because of user should be change it.

Answer Source

You are getting a client side validation error as a result of jquery.validate.js which uses the following to validate the value (which only allows the . character as the decimal separator.

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

You can use the jquery.validate.globalize.js plugin (refer this article for more detail) or you can add your own script to modify the validator, for example (include this after jquery.validate.js)

$.validator.methods.number = function (value, element) {
   return this.optional(element) || $.isNumeric($(element).val().replace('/', '.'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download