Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should ASP.NET MVC 2 validation go: in the model or viewmodel classes?

I am using automapper to map my models to viewmodel classes to pass to my view.

My question really is where should the validation go? I was planning on using the MetaData decorations - a feature of mvc 2.

But either in the model or viewmodel? Or in both places?

like image 559
Martin Avatar asked Nov 01 '10 15:11

Martin


2 Answers

Validation should be done minimum at the View Model because this is what you receive as action argument and contains the user input. You could also have validation at the model.

like image 92
Darin Dimitrov Avatar answered Sep 25 '22 15:09

Darin Dimitrov


My answer would be ViewModel because Model can change (for example from using Linq2SQL to EF). This way when you plug another Model you still have your validation intact.

like image 33
Arief Avatar answered Sep 22 '22 15:09

Arief