问题描述
我使用MVC 5.0结果
我设置了文化配置:
I'm using MVC 5.0
I set the culture in config:
<system.web>
<globalization uiCulture="fa-IR" culture="fa-IR" />
</system.web>
我有一个模型如下:
I have a model as the following:
public class MyModel
{
[DisplayName("NbatPersent")]
[Range(0, 100)]
public double NbatPersent{ get; set; }
}
MVC节目,如针对 NbatPersent
值 22/5
,当我想提交本的形式验证警报我字段NbatPersent必须是一个数字。
。它不能转换 22/5
到 22.5
这将是确定的,如果我输入22.5,但如果该属性有值转换。
到 /
MVC shows the NbatPersent
value in View like 22/5
and when I wanna submit form the form validator alert me The field NbatPersent must be a number.
. It can't convert 22/5
to 22.5
It will be OK if I enter 22.5 but if the property has a value it convert .
to /
我怎么能转换所有数值属性文化的en-US
显示如 22.5
不像值 22/5
。
How can I convert all numeric properties' culture to en-US
to show value like 22.5
not like 22/5
.
编辑:
我使用 @ Html.TextBoxFor
来显示小数属性,因为用户应该改变它。
I'm using @Html.TextBoxFor
to show the decimal property because of user should be change it.
推荐答案
您已经看到了客户端验证误差 jquery.validate.js
的结果,它使用下面要验证的值(只允许。
字符作为小数点分隔符。
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);
},
您可以使用 jquery.validate.globalize.js
插件(指的),也可以添加自己的脚本来修改验证,例如(包括此之后的jQuery .validate.js
)
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('/', '.'));
}
这篇关于在MVC十进制符号的问题,C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!