I think I get the idea of the ViewModel
in MVC
but, when doing updates and deletes, it seems like there should be a distinct model for posting to the controller. I noticed the default razor controllers use ViewBag
to hold Selectlists.
I guess that makes the ViewModel
(domain entities really) reusable on the return trip because it is stripped of unnecessary data. But it seems like resorting to ViewBag
doesn't make sense when using view models because the view model can contain the Selectlist
s and such.
So my question is what kinds of patterns are there for making distinct "posted data" models? (got this term from Esposito's MVC 2 book) And how should the posted data model be related to the view models? For example, it seems like I will try including the posted data models with the view models. I'm new to MVC
and not coming from a web-forms
background either. I would really like to understand the best patterns for modeling the data that will be sent to the controller.
Often I use the same view model for passing it to the Edit/Update view and receiving it in the POST action. Here's the commonly used pattern:
As far as the mapping between the domain model and view models is concerned I would recommend you AutoMapper.