Standardize error messages in DataAnnotations in ViewModels classes

1

It is possible to standardize the error messages used in DataAnnotations, so that you do not have to pass a string like:

[Required(ErrorMessage = "Este campo é obrigatório.")]

But you can use more or less like this:

string mensagem = "Este campo é obrigatório.";
[Required(ErrorMessege = mensagem)]
    
asked by anonymous 18.09.2017 / 13:44

1 answer

1

Yes, use a Resource for this. In my case it's called Error.resx:

[Required(ErrorMessageResourceName = "CampoObrigatorio", ErrorMessageResourceType = typeof(MensagemErro))]

Basically I say that I have a resource of type MensagemErro with a name called CampoObrigatorio .

In my example I still use a {0} to concatenate a name, but ignore it in your case.

    
18.09.2017 / 15:36