“十进制字段必须是数字" [英] "The decimal field must be a number"

查看:92
本文介绍了“十进制字段必须是数字"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为该问题找到一个可能的简单解决方案.网络上找到的所有解决方案都不适合我.我的模型中有一个价格字段,该字段可以是一个十进制值,例如30,5(使用逗号).但是验证返回一个错误字段必须是数字".

I'm trying to find a possible easy solution for this issue. All the solutions found in web are not working for me. I have in my model a price field which can be a decimal value like 30,5 using comma. But the validation returns me an error "The 'field' must be a number".

我正在使用MVC中使用的默认验证,我想它是使用jQuery.有什么办法可以避免这个问题?

I'm using the default validation used in MVC that I suppose it is using jQuery. Is there any way to avoid this issue?

型号代码为:

 [Range(0.0, Double.MaxValue, ErrorMessage = "Out of range")]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }

推荐答案

我认为该错误来自客户端验证,而不是服务器端验证.如果它确实到达了服务器,那么指定区域性就可以了.但是,ASP.NET的不干扰验证实际上使用jQuery Validation插件,如您在此处所见,在文档页面中,对于number验证程序,只有作为小数点分隔符的.会验证.您可能只需要从字段中删除数字验证规则即可.

I believe the error is coming from client-side validation, not server-side. If it actually got to the server, you should be fine having the culture specified. However, ASP.NET's unobtrusive validation actually use the jQuery Validation plugin, and as you can see here at the documentation page for the number validator, only a . as the decimal separator will validate. You may simply have to remove the number validation rule from the field.

$("#Price").rules("remove", "number");

更新

这篇文章可能会帮助您: http://blog. icanmakethiswork.io/2012/09/globalize-and-jquery-validate.html .它说明了如何从本质上进行猴子补丁jQuery验证,从而真正关注文化.

This post may help you: http://blog.icanmakethiswork.io/2012/09/globalize-and-jquery-validate.html. It explains how to essentially monkey-patch jQuery Validation to actually pay attention to culture.

这篇关于“十进制字段必须是数字"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆