如何更改默认的"字段必须是数字" [英] How to change the default "The field must be a number"
问题描述
我工作的一个MVC 3应用程序。一个在模型中的字段的类型是双的和定义如下:
I'm working on an MVC 3 application. One of the fields in the model is of type double and defined as follows:
[Required(ErrorMessageResourceName = "ListingItemPriceRequired", ErrorMessageResourceType = typeof(ErrorMessages))]
[Display(Name = "DisplayListingItemPrice", ResourceType = typeof(Display))]
[Range(1, 500000000, ErrorMessageResourceName = "ListingItemPriceNotWithinRange", ErrorMessageResourceType = typeof(ErrorMessages))]
public double Price { get; set; }
不过,当我输入一个数字的值与一些尾随空格,如342,我得到默认的消息这个领域的价格必须是一个数字。
Still, when I enter a value of a number with some trailing spaces like "342 ", I get the default message "The field price must be a number".
即使是在价格输入字段验证属性的东西为数据-VAL-号。
Even the validation attribute on the Price input field has something as "data-val-number".
感谢
推荐答案
默认消息被深深烤成的框架,作为一个字符串资源。它是由试图将字符串值double类型绑定时的默认模式添加粘合剂。所以,如果你想改变这种默认的消息,你可以写一个自定义模型粘合剂。下面是我写的有同样的问题,DateTime类型的例子: http://stackoverflow.com/a/7836093/29407一>
The default message is baked deeply into the framework, as a string resource. It is added by the default model binder when trying to bind the string value to a double type. So if you want to change this default message you could write a custom model binder. Here's an example I wrote for the DateTime type which has the same issue: http://stackoverflow.com/a/7836093/29407
这篇关于如何更改默认的"字段必须是数字"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!