0

I have an MVC app where the I mostly pass the Models to the controllers. I have a handful of situations where I use viewmodel But most CRUD operations are done directly. I have data annotations on each property in the model class which I use to validate the model. However recently I have heard that a better practice is to use ViewModels- one for each view. Which seems to me like unnecessary and just a programmers nightmare as far as maintenance is concerned. I was wondering if anyone agreed or disagreed with me on this?

superartsy
  • 489
  • 1
  • 11
  • 27
  • I use view models sparingly, mainly to help a view build up. for example, if you have a view that needs a model plus some name value pairs for a select list, then i will create a view model that has both the model and this list in, it reduces the need for tempdata and viewdata to pass stuff around. otherwise, i use the domain models where possible – Slicksim Apr 05 '13 at 13:47
  • 5
    I try not to use my domain model directly, it will often have more properties than are needed by any view and certainly the methods are not required. Also as requirements change it helps to isolate where changes need to be made – Simon Martin Apr 05 '13 at 13:49
  • 1
    It's been discussed a lot already: http://stackoverflow.com/questions/5248746/asp-net-mvc-viewmodels-versus-domain-entities http://stackoverflow.com/questions/664205/viewmodel-best-practices Note recommendations to use AutoMapper in order to avoid what you refer to as a maintenance nightmare. – Yakimych Apr 05 '13 at 13:54

1 Answers1

2

I personally think the terms "Model" and "ViewModel" are often confused.

In Microsoft-Tutorial-MVC-World a "Model" maps directly to a database "Entity" and you can easily perform CRUD operations. But in a medium-large scale ASP.NET MVC app you would very rarely do this.

Therefore, you would probably build up a set of "View-related-business-objects" to pass backward and forward to and from Views. Call them "Models" if you like. Call them "ViewModels" if you like. Doesn't really matter. They perform the same function. They both put the "M" in "MVC".

LiverpoolsNumber9
  • 2,384
  • 3
  • 22
  • 34