How can I use my custom method to retrieve string resources and use on properties? For example:
public class Friend
{
[Required(ErrorMessage = "Invalid Name")]
[Display(Name = "Friend's name")]
public string Name { get; set; }
}
The default resource provider (if I set Resource开发者_StackOverflow社区Type
/ErrorMessageResourceType
) will look for a class with a public property, but my resource files doesn't have a generated class, they are plain XML.
I have a method ResourceHelper.Get(key)
which returns the value I expect. How can I change it to use my way of retrieving the resources?
Here are few different ways to do try in your case. Fourth approach sounds promising.
http://carrarini.blogspot.com/2010/08/localize-aspnet-mvc-2-dataannotations.html
Approaches to solve the problem:
- Writing a Custom Build Provider, which compiles resources from the Database.
- Creating a Dynamic Object, which transforms the property name to a key for the Resource Manager.
- Extending Attributes of Data Annotations.
- Using a T4 Template for generating a class containing the resources.
[EDIT}
- For your question regarding Dynamic Object
In order to do that, you will have to create friend object dynamically. That means inheriting Friend from DynamicObject class and implementing TryGetMember() and TrySetMember(). These properties internally works on Resource objects "HttpContext.GetLocalResourceObject" and let you create dynamic Friend class with localized properties. After that, you need to implement custom ModelBinder to use wrap around it.
This guy found better work around to that:- MVC3 ModelBinder for DynamicObject
精彩评论