加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 字段必须是数字 如何将此消息更改为其他语言?

发布时间:2020-12-15 18:54:27 所属栏目:asp.Net 来源:网络整理
导读:如何更改所有int字段的消息,这样,而不是说: 该字段必须是英语数字,它显示: El campo tiene que ser numerico的西班牙语。 有没有办法? 解决方法 如果你碰巧使用ASP.NET MVC 4以上,请检查此帖子: Localizing Default Error Messages in ASP.NET MVC a
如何更改所有int字段的消息,这样,而不是说:

该字段必须是英语数字,它显示:

El campo tiene que ser numerico的西班牙语。

有没有办法?

解决方法

如果你碰巧使用ASP.NET MVC 4以上,请检查此帖子:

Localizing Default Error Messages in ASP.NET MVC and WebForms

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

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

在Visual Studio中的解决方案资源管理器中右键单击您的ASP.NET MVC项目,然后选择Add =>添加ASP.NET文件夹=> App_Global资源。

现在在此文件夹中添加一个名为Messages.resx的.resx文件。

最后,在.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.

你应该很好去。

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读