且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC:ValidationAttribute和本地化的ErrorMessage

更新时间:2023-02-25 18:17:31

我遇到了一些问题,利用本地化错误消息和DataAnnotations,pretty你的情况多的是相同的。虽然我没有显示错误消息的空白之中结束了,我总是有一些例外抛出。解决方法对我来说是:

I had some problems with using localized error messages and DataAnnotations, pretty much identical to your situation. Although I didn't end up with blank being displayed as error messages, I always got some exceptions thrown. Solution for me was:


  1. 在App_GlobalResources文件/打开Errors.resx资源文件,改变它的属性生成操作:嵌入式的资源,自定义工具:PublicResXFile codeGenerator,自定义工具命名空间:资源(视觉工作室做一些奇怪的autogenerating这些,所以回来了,事情都OK检查)

  1. in App_GlobalResources/Errors.resx open the resource file, change it's properties to Build Action: Embedded Resource, Custom Tool: PublicResXFileCodeGenerator, Custom Tool Namespace: Resources (visual studio does something weird in autogenerating these, so check back again that things are ok)

更​​改资源文件的访问修饰符公共

Change the resource file's access modifier to Public

检查从生成(Errors.Designer.cs)的命名空间和访问修饰符是正确的code文件。

Check from the code file generated (Errors.Designer.cs) that the namespace and access modifier are correct.

试用

从Model类我的例子:

My example from the Model class:

[Required(ErrorMessageResourceType = typeof(Resources.Errors), ErrorMessageResourceName="ResponseMessageRequired")]
public string message { get; set; }