Search code examples
c#asp.net.netasp.net-mvcresx

Add resx text to model validation


I want to replace the hard-coded message from string from the resx file in my project. When I have simple text like column header I do it like following and it's working OK ("Name" is the key in the resx file).

[Display(Name = "Name", ResourceType = typeof(PStr)),Required]

Now I want to change the Invalid name text with resx that I've gotten.

[RegularExpression(@"^\s?[a-zA-Z0-9_-]+\s*$", ErrorMessage = "Invalid Name")]

How should I do that?

When I try it like the following I get an error at compile time:

[RegularExpression(@"^\s?[a-zA-Z0-9_-]+\s*$", ErrorMessage = "ERROR_Valid", ResourceType = typeof(PStr))]

Solution

  • When using ErrorMessageResourceType you should be using ErrorMessageResourceName btw.