C# – ASP.NET MVC4: An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type

asp.net-mvcasp.net-mvc-4cdata-annotations

I had the following code:

    [Required(ErrorMessage = MessageModel.translateMessage("required")))]
    [Display(Name= MessageModel.translateMessage("id"))]
    public string user_id { get; set; }

I am trying to make the error message dynamic but I get error when compiled.:

"An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type."

Any solution for this issue?

Best Answer

First you create a Resource .resx file this will contain your localised strings.

When you declare the attribute you set the ResourceType argument. This causes the Name, ShortName and Description arguments to be used as a resource key instead of a value.

[Display(Name = "GenreName", ShortName = "GenreShortName", Description = "GenreDescription", ResourceType = typeof(MyResources))]
public string Genre { get; set; }