asp.net-mvc 该字段必须是数字。如何将此消息更改为另一种语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12099466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:12:35  来源:igfitidea点击:

The field must be a number. How to change this message to another language?

asp.net-mvcvalidationlocalizationnumbersdata-annotations

提问by Diego_DX

How can I change that messages for all intfields so that instead of saying:

如何更改所有int字段的消息,而不是说:

The field must be a numberin English, it shows:

The field must be a number在英语中,它显示:

El campo tiene que ser numericoin Spanish.

El campo tiene que ser numerico在西班牙语中。

Is there are a way?

有办法吗?

回答by Leniel Maccaferri

If you happen to be using ASP.NET MVC 4 onwards, check this post:

如果您碰巧从 ASP.NET MVC 4 开始使用,请查看这篇文章:

Localizing Default Error Messages in ASP.NET MVC and WebForms

本地化 ASP.NET MVC 和 WebForms 中的默认错误消息

Basically you have to add the following piece of code in your Application_Start()method in Global.asax:

基本上你必须在你的Application_Start()方法中添加以下代码Global.asax

 ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
 DefaultModelBinder.ResourceClassKey = "Messages";

Right click your ASP.NET MVC project in Solution Explorer inside Visual Studio and select Add => Add ASP.NET Folder => App_GlobalResources.

在 Visual Studio 内的解决方案资源管理器中右键单击您的 ASP.NET MVC 项目,然后选择Add => Add ASP.NET Folder => App_GlobalResources.

Now add a .resxfile inside this folder called Messages.resx.

现在.resx在这个文件夹中添加一个名为Messages.resx.

Finally add the following string resources in that .resxfile:

最后在该.resx文件中添加以下字符串资源:

Name                   Value
====                   =====
FieldMustBeDate        The field {0} must be a date.
FieldMustBeNumeric     The field {0} must be a number.
PropertyValueInvalid   The value '{0}' is not valid for {1}.
PropertyValueRequired  A value is required.

You should be good to go.

你应该很高兴去。

Note that the value you're interested in is the FieldMustBeNumeric. To localize it to Spanish, you have to add another resource file named Messages.es.resx. In this specific .resxfile replace the resource value with:

请注意,您感兴趣的值是FieldMustBeNumeric. 要将其本地化为西班牙语,您必须添加另一个名为Messages.es.resx. 在此特定.resx文件中,将资源值替换为:

Name                Value
====                =====
FieldMustBeNumeric  El campo {0} tiene que ser numerico.


If you happen to be using ASP.NET MVC 3 downwards, this solution can help you achieve the same result: https://stackoverflow.com/a/2551481/114029

如果您碰巧向下使用 ASP.NET MVC 3,此解决方案可以帮助您实现相同的结果:https: //stackoverflow.com/a/2551481/114029

回答by M.Azad

you can set your custom message for your validation.

您可以为您的验证设置自定义消息。

 [RegularExpression("\d{9}",ErrorMessage="El campo tiene que ser numerico")]
 public decimal UnitPrice { get; set; } 

回答by Dilip0165

If you want to specify custom message for each Integer , double and float . you can use Range Attribute with String as below.

如果要为每个 Integer 、 double 和 float 指定自定义消息。您可以将范围属性与字符串一起使用,如下所示。

    [Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "YearOfEstablishmentRequired")]
    [Range(0, int.MaxValue, ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "ValidYearOfEstablishment")]
    [Display(Name = "Year Of Establishment")]
    public string YearOfEstablishment { get; set; }

Now as above you can specify custom message for each and every propery .

现在如上所述,您可以为每个属性指定自定义消息。