Who populates the ViewModel in ASP MVC 5

asp.net-mvc c# entity-framework viewmodel


In an ASP MVC 5 architecture (C#, EF), who is responsible for populating the values, for example, if we havePurchaseRecordsViewModel , PurchaseRecords Domain Model , PurchaseController

  • Does the viewmodel's own viewmodel go in the place where the code to populate data (time, cost, etc.)PurchaseRecordsViewModel ?

  • The code may also be placed in the Action method of thePurchaseController

10/15/2014 8:11:54 PM

Accepted Answer

10/31/2018 3:50:11 AM

Popular Answer

Typically, view models are essentially meaningless collections of characteristics. If you don't have a service layer, your action method usually handles populating view models.

Consider the roles in this manner.

  • An immediate mapping to a database table is a domain model.
  • A view model is a group of attributes required to render a view.
  • A view model is filled by a service layer using one or more domain models.
  • Additionally, a service layer may use a view model to construct or modify one or more domain models.
  • The link between the two is a controller action mechanism. It uses a service layer's GET command to request a view model before passing it to the view. Additionally, these action methods accept (POST) a view model and send it to the service layer, which will do any necessary operations on it.

I frequently hear the question, "Why can't I utilize domain models for a view?" You can, but usually you run into problems like needing information from several domain models, not needing all the characteristics in the domain model, and worrying about unintended attributes being updated on the domain model.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow