Is it best practice to put data validation annotations in the Model or View Model? What are the advantages/disadvantages of one approach over the other?
Curious to see where everyone is putting their validation, I am currently doing it in the model project. However I have seen a few people say this is not best practice.
As far as best practices is concerned I would say: in neither of them. Validation should be separate. Frameworks such as FluentValidation allow you to completely separate your validation logic from your models. But to answer your question I would put validation into View Models as those are the classes you are binding your controller actions to. You could also have multiple View Models that are tied to the same model but with different validation rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With