This article was summarized from: Http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications
ViewModel This concept is not just in the MVC pattern, you will see this in many articles on MVC, MVP, MVVM, and this concept can be mentioned in any technology, such as ASP. NET, Silverlight, WPF, or MVC ... Now let's talk about how to use it in MVC.
What is ASP. NET MVC ViewModel?
In general, when we pass data to the view, it's a model, and when there's some extra data we'll use viewbag and so on, but we can use ViewModel to integrate these together. That is: ASP. ViewModel allows you to integrate one or more data model and resources into an object so that the view is optimized when using model, and the following illustration shows the concept of ViewModel:
The purpose of using ViewModel is to let the view single object to render, on the other hand can reduce the UI display logic code, this is necessary, that is to say view only task is to render a single ViewModel object, In order to have a clear separation between concerns, decoupling means that your program is designed to be better, so we need to place the code for the data processing in the location it should be, away from view, controller.
Using ViewModel in MVC will make it easier to separate your program components from the maintenance, remembering that unit testing refers to testing small units. The following is a list of when you want to use ViewModel:
1. Incorporating dropdown lists of lookup data into a related entity
2. Master-detail Records view pagination:
3. Combining actual data and paging information
4. Shopping cart or User profile widget
5. Dashboards with multiple sources of disparate
6. Data Reports, often with aggregate data
Create a ViewModel
Although ViewModel contains several entities, it is still just a class, nothing special, where it can be placed:
1, a folder called ViewModels, this folder can be placed in the root directory of the project;
2, as a DLL, we reference it from the MVC project;
3. Service layer placed in a separate project;
The first way is undoubtedly the simplest one, we just need to create a folder, and then create a class on the line.
In order to create a customerviewmodel, we want to combine the customer and statedirctionary types as attributes to assemble a Customerviewmodel class with the following code:
public class Customerviewmodel { public Customer Customer {get ; set public statesdictionary states {get ; set ; } public Customerviewmodel (Customer customer) { Customer = customer; states = new Statesdictionary (); } }
Send ViewModel to Veiw
Of course we started with the controller, sending the ViewModel to view and we're sending the normal model to view the same way, because ViewModel is just a class, View does not know or care where the model or ViewModel came from, you can create an instance of ViewModel in the controller, in order to keep the controller neat, no additional code, What needs to be done in the controller is to get the value of model or ViewModel, no other:
Public ActionResult Edit (int ID) { = context. Customers.single (x = x.id = = Id) ; var New Customerviewmodel (customer); return View (Customerviewmodel);}
And then the view to render the ViewModel
In order for view to know which object we passed, we need to point the @model keyword to ViewModel as if you were specifying a generic model.
@model FourthCoffee.Web.ViewModels.CustomerViewModel
Code to invoke the data:
class="editor-label"> @Html. labelfor (model=class ="editor-field"> "=" model. Customer.firstname) @Html. validationmessagefor (model = model. Customer.firstname) </div>@* ... View code continues rendering Properties ...
Tips for using ViewModel:
1, because the use of ViewModel requires manual mapping, but when the ViewModel is very complex, this will become more difficult, this time, we can use the AutoMapper to simplify the work, AutoMapper will allow you to seamlessly create mappings between ViewModel and models, in addition to: Poco Generator, EF Poco Templates
2. Put only the attributes you use to render on the view in the ViewModel
3, with view to guide the creation of ViewModel attributes, so as to better render and maintain
The ViewModel in MVC