X

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.

Liked this post? Empower your friends by sharing it!
Categories: ASP.NET

View Comments (3)

Related Post