Localizing ViewModel in ASP.NET MVC4 with Resources

First, I want to clarify that I'm currently using the ASP.NET MVC's Model entities as ViewModel because my project is based on MVCVM, so I'm not simply confusing the two.

Anyway MVC automatically creates me a few attributes for ViewModel entities like the following (from the wizard, localized in Italian)

public class LoginModel
    [Display(Name = "Nome utente")]
    public string UserName { get; set; }

    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Memorizza account")]
    public bool RememberMe { get; set; }

Replacing the Display attribute with [Display(Name = LoginViewModelResources.lblUsername)] causes compilation error: "Argument must be a constant expression, typeof expression or matrix creation expression". In a few words, a property reference is a no-no.

The Razor view uses tags such as the following for generating HTML

@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)

How can I localize the ViewModel in order to display the correct messages at front-end?


Like this:

[Display(Name = "lblUsername", ResourceType = typeof(LoginViewModelResources))]
public string UserName { get; set; }

For this to work you must define a LoginViewModelResources.resx file with Custom Tool=PublicResXFileCodeGenerator (you set this in the properties of the resource file) and containing a label lblUsername which will hold the localized resource.

Need Your Help

Sql - How to make this query

sql sql-server-ce

I am trying to make a query for SQL Compact

I require assistance with the usage of glob()

php glob

So, I'm trying to minimize the size of the bootstrap file (the core file), in this file I require many libraries and interfaces, I tried using the foreach(glob()) hack to lightweight it, however, my

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.