asp.net-mvc - MVC3 编辑十进制字段和本地化

标签 asp.net-mvc asp.net-mvc-3 localization number-formatting

我的区域设置使用逗号 ,,而不是点 . 作为小数分隔符。

在 MVC3 中,当我打开编辑 View 时,小数值用

显示
@Html.EditorFor(model => model.MyDecimalVal)

该值显示正确。

当我用逗号输入值时,出现错误“值不是数字”,如果我用点输入值,则不会出现错误,但实际上没有保存任何值。

遇到这种情况该如何处理?

最佳答案

This blog post建议覆盖默认的 jQuery 验证数字和范围规则,以启用逗号小数分隔符的客户端支持。

To fix these problems, we can take the default implementation from the jquery.validate.js file for the range() and number() functions. We then create another .js file (say jQueryFixes.js), in which we override these default functions with ones that contain support for the comma as a decimal separator. The contents of the file should be something like this:

$.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);
}

关于asp.net-mvc - MVC3 编辑十进制字段和本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8101589/

相关文章:

asp.net-mvc - 在 MVC 中定义 html5 数据属性

c# - ASP.NET Massive - 如何创建数据库以显示 VIEW

asp.net-mvc-3 - 如何防止ASP.NET MVC应用程序中的DoS攻击?

ios - 如何处理部分翻译的 Localizable.strings 文件

asp.net - 我应该使用 301 进行站内重定向吗?

javascript - Dymo条码标签打印

c# - Tesseract OCR - 如何训练这样的图像

asp.net-mvc - 如何将自定义数据属性和类添加到 `@Html.EditorFor` ?

jquery - MVC 3 JSON 调用在 IIS 中不起作用

ios - 根据NSUserDefaults的设置进行iOS App本地化