-1

I have Model with properties with Require attribute. I also have resource file which I store some values. The idea is to use the resource file as values to attribute property parameter. But the following error message is thrown Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type Credentials.cs. Any idea what is the problem and how to solve it ?

Example:

[Required(ErrorMessage = STT.Properties.Resources.Require_Username)]
public string Username { get; set; }
TheChampp
  • 1,337
  • 5
  • 24
  • 41

1 Answers1

3

This is the correct way to use resources in required attribute:

[Required(ErrorMessageResourceName = "Require_Username", ErrorMessageResourceType = typeof(STT.Properties.Resources))]
erikscandola
  • 2,854
  • 2
  • 17
  • 24
  • It is working now. What was the problem ? – TheChampp May 05 '16 at 12:36
  • 1
    You can't pass directly a non constant value to an attribute, so you need to use, in this case, `ErrorMessageResourceName` and `ErrorMessageResourceType` to pass resource value. [Here](http://stackoverflow.com/questions/14244784/use-int-constant-in-attribute) you can find more info. – erikscandola May 05 '16 at 12:43