Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Model vs ViewModel

OK, I have been hearing discussion about "ViewModels" in regards to MS's ASP.NET MVC.

Now, that is intended to be a specific kind of Model, correct? Not a specific kind of View.

To my understanding, it's a kind of Model that has a specific purpose of interacting with the View? Or something like that?

Some clarification would be appreciated.

like image 844
Qcom Avatar asked Oct 31 '10 01:10

Qcom


People also ask

What is difference between model and ViewModel in MVC?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

What is model and ViewModel in MVC pattern?

What is ViewModel? ViewModel in the MVC design pattern is very similar to a "model". The major difference between "Model" and "ViewModel" is that we use a ViewModel only in rendering views. We put all our ViewModel classes in a "ViewModels" named folder, we create this folder. Understand it with an example.

Why do we use ViewModel in MVC?

What ViewModel is. In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization.

What is the difference between View and ViewModel?

VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.


1 Answers

Essentially Model and View Model are both simple classes with attributes.

The main objective of these classes are to describe (to "Model") an object for their respective audiences that are respectively the controller and the view.

So you are completely right when you say

To my understanding, it's a kind of Model that has a specific purpose of interacting with the View

So, while Model classes are effectively Domain Entities that your application interact with, View Models are simple classes that your views interact with.

Hope it helps :)

Update:

Microsoft has developed a specialized version of the Presentation Pattern by Martin fowler largely based on the Model-View-Controller and called it Model-View-ViewModel (MVVM) for PF application. This pattern is targeted at modern UI development platforms where UI developers have different requirements based more on business logic than traditional developers. Have a look here for a bit of theory

like image 82
Lorenzo Avatar answered Oct 23 '22 03:10

Lorenzo