Search code examples
c#asp.net-mvcresx

Assigning attributes from resx Files


string thing = ComparaSave.Resources.Message.EmailAdrRequired;
[Required(ErrorMessage = thing)]    <---- The problem!!
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
[StringLength(100)]
[EmailAddress]
public string Email { get; set; }

Throws the error:

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

How can I read a resource file into an attribute such as this?


Solution

  • You need to use :

    [Required(ErrorMessageResourceName= "FullNameRequired", ErrorMessageResourceType = typeof(MyResource)]
    

    ErrorMessage is for string only.