I’m localizing my asp.net Mvc app. The strings are placed in resource files. I’m decorating my models with the System.ComponentModel.DataAnnotations.DisplayAttribute attribute like for example:
public class User
{
public virtual Guid Id { get; set; }
[Display(ResourceType = typeof(ModelRes.User), Name = "LogonName")]
public virtual string LogonName { get; set; }
[Display(ResourceType = typeof(ModelRes.User), Name = "Password")]
public virtual string Password { get; set; }
}
I have one resource file per model(class), and the User.resx looks like this:
LogonName | "Logon name"
Password | "Password"
Now imagine that for a lot of properties, you get the idea. What I would like to do instead is this:
[CustomDisplay(typeof(ModelRes.User))]
public class User
{
public virtual Guid Id { get; set; }
public virtual string LogonName { get; set; }
public virtual string Password { get; set; }
}
and still be able to use this in my (razor) view:
@Html.LabelFor(m => m.LogonName)
Any experiences with implementing something like this?
You will have to write a custom ModelMetadataProvider.
Then you will have to set the
DisplayNameproperty of theModelMetadatavariable using your custom attribute and property name.Here are some examples/tutorials
ASP.NET MVC 3 Service Location, Part 7: Model Metadata
Customizing ASP.NET MVC 2 Metadata and Validation