2

I use validation attributes and I want to localize my error messages. I found this:

[RegularExpression(
    "^[a-zA-Z0-9_]*$", 
    ErrorMessageResourceType=typeof(Resources.RegistrationModel),
    ErrorMessageResourceName= "UsernameError"
)]

You can even do it with a resource string like this:

"{0} is in an invalid format."

And it will fill in the property name. Almost what I want, however instead of the property name I want to pass a specific string like "User name" or "Phone number" (actually I want to get those strings from the resource file as well).

Is this possible? Or do I have to have a lot of very similar strings in my resource file?

Community
  • 1
  • 1
Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61

1 Answers1

4

You should use Display attribute.

Example

[Display(Name = "Description", ResourceType = typeof(Messages))]
public MessageModel Description { get; set; }

Sure, Name parameter should be named like string in resources.

user2216
  • 809
  • 1
  • 8
  • 24