Standardize error messages in Dataannotations in Viewmodels classes

Asked

Viewed 274 times

1

It is possible to standardize the error messages used in Dataannotations, so that it is not necessary to pass a string like:

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

But you can use it more or less like this:

string mensagem = "Este campo é obrigatório.";
[Required(ErrorMessege = mensagem)]

1 answer

1

Yes, use a Resource for that. In my case it’s called Mensagemerro.resx:

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

Basically I say I have a resource like MensagemErro with a name called CampoObrigatorio.

Imagem de como ficaria o resource

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.