MVC Validation using Data Annotations - Model classes or View Model classes?

前端 未结 4 1069
心在旅途
心在旅途 2021-02-14 07:41

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 w

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-14 08:41

    If you follow single responsibility then it should probably go into it's own component. That being said, if you want to make a short cut it's okay to but it in the ViewModel. It definitely shouldn't go in the model though. Your model should be "pure" data. No business rules and validation is business rules.

提交回复
热议问题