Either ErrorMessageString or ErrorMessageResourceName must be set, but not both
Found weird error when adding translations support to some ASP.NET MVC models. I’m working on application that uses authentication forms generated by Visual Studio. As it turns out this is known bug with data annotations in .NET 4.5. Solution is here.
Suppose you have property with validation attributes:
[Required(ErrorMessageResourceName = “ValidateEmailRequired”, ErrorMessageResourceType = typeof(UserResources))]
[Display(Name = “Email”, ResourceType = typeof(UserResources))]
[EmailAddress(ErrorMessageResourceName = “ValidateEmailIncorrect”, ErrorMessageResourceType = typeof(UserResources))]
public string Email { get; set; }
This exception comes from Required and EmailAddress attributes. All you have to do is to add ErrorMessage = null and everything works again
[Required(ErrorMessage = null, ErrorMessageResourceName = “ValidateEmailRequired”, ErrorMessageResourceType = typeof(UserResources))]
[Display(Name = “Email”, ResourceType = typeof(UserResources))]
[EmailAddress(ErrorMessage = null, ErrorMessageResourceName = “ValidateEmailIncorrect”, ErrorMessageResourceType = typeof(UserResources))]
public string Email { get; set; }
Notice that you don’t have to add ErrorMessage property to DisplayAttribute as it doesn’t support it.
Hey Gunnar, how is it going?
Found your old post and figured out I met this guy in Serbia on MVP Summit :)
Thanks for the post by the way! Nice catch!
Hi!
I’m fine. How are you? Did this post help you?
so useful. Thank you