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.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    3 thoughts on “Either ErrorMessageString or ErrorMessageResourceName must be set, but not both

    • July 5, 2018 at 10:57 am
      Permalink

      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!

    • April 4, 2019 at 2:46 am
      Permalink

      so useful. Thank you

    Leave a Reply

    Your email address will not be published. Required fields are marked *