Asp.net-Mvc

該欄位必須是數字。如何將此消息更改為另一種語言?

  • August 23, 2012

如何更改所有int欄位的消息,而不是說:

The field must be a number在英語中,它顯示:

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

有沒有辦法?

如果您碰巧使用的是 ASP.NET MVC 4 以後的版本,請查看這篇文章:

在 ASP.NET MVC 和 WebForms 中本地化預設錯誤消息

Application_Start()基本上你必須在你的方法中添加以下程式碼Global.asax

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

在 Visual Studio 的解決方案資源管理器中右鍵點擊您的 ASP.NET MVC 項目,然後選擇Add => Add ASP.NET Folder => App_GlobalResources.

現在.resx在這個文件夾中添加一個名為Messages.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,此解決方案可以幫助您達到相同的結果:https ://stackoverflow.com/a/2551481/114029

引用自:https://stackoverflow.com/questions/12099466