Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 14, 2009 06:29 PM by mirajaros
Jun 10, 2009 02:20 PM|LINK
Could anyone pass on a bit of advice about using ErrorMessageResourceName and ErrorMessageResourceType with resource files. I am using an MVC app and we are setting the data annotations in the Data Model. Should the resource file (in a App_LocalResources
folder) be at the Data Model or where the View is contained?
Also, could anyone give a concrete example of
[Required(ErrorMessageResourceName= "FullNameRequired", ErrorMessageResourceType = typeof(MyResource)]
For example, what do "FullNameRequired" and "MyResource" stand for ...
Jun 16, 2009 07:01 PM|LINK
I don't think it matters exactly where the RESX file is as long as you can "see" the type from wherever you're using the Data Annotations. For example, I think you can have your RESX file alongside wherever you're using the Required attribute (such as in
the Models folder). If you're getting Intellisense for the resource type then it should work.
The App_GlobalResources and App_LocalResources folders are primarily intended for resources that are used inside your view pages (though in some cases you can use them in other places).
Jun 24, 2009 11:07 AM|LINK
What I didn't understand was that the resource file must be converter to a class before being able to reference it in the typeof of the ErrorMessageResourceType in the data annotation. I hadn't encountered this ability before of turning a resource file into
I guess, it's a little difficult to see the advantage of this extra step as if one changes the resource file, then the class must be re-generated to see the change. Our MVC app has very little in the way of view state being passed back and forth so using
the server side approach to resource files may be an acceptable compromise.
Thanks for taking the time to respond to my initial query.
Jun 24, 2009 08:03 PM|LINK
The default behavior of Visual Studio when you put a .resx file into a project is to automatically create the .designer.cs file with the class in it, and whenever you update the .resx file, the class is automatically updated.
DataAnnotations uses this type-based resource system because its scope is larger than "web applications" (for example, it also works with Silverlight applications with RIA Services).
Jun 26, 2009 09:52 AM|LINK
For some reason, when I added resource files to the project, the .designer.cs class wasn't added. I'll have another look at this and see what the story is, but thanks for pointing me in this direction. It makes a lot more sense now.
Jun 29, 2009 08:12 AM|LINK
You definitely put me on the right track with the .designer.cs file. I found out about setting the 'access modifier' to public and also setting the build action of the resource file to 'embedded resource'. This has worked a treat in terms of displaying the
data annotations on screen.
However, how do I now localise this content? I have added another resource file with a resources.fr-FR.resx name but the localised content is not being picked up.
Jun 29, 2009 06:26 PM|LINK
ASP.NET by default does not set the current culture based on the browser request.
You have two choices:
There is a lot of discussion about the impact of automatic browser culture that's worth reading:
Jun 30, 2009 11:39 AM|LINK
Thanks Brad, I using option 2 already, but it's not working for the data annotations. It's working fine for the MVC ui in that when i set the language in the browser to french, the content in the .fr-FR.resx appears perfectly. However, this doesn't work
for the data annotations of the data model.
I'll keep looking into it and I appreciate your expert help up to this point. It's been super ...
Jul 09, 2009 08:53 AM|LINK
I needed to add the following entry under <server.web> in the web.config of my mvc app ...
<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />
Now the data annotations are finally appearing localised
Sep 14, 2009 06:29 PM|LINK
I want to post some advice for everybody who has problem with displaying own errors via resources.