ASP.NET MVC Terminology Disables Me - Why "ViewModel"?

I am new to ASP.NET MVC, but previously used many of the Model-View-Controller frameworks.

I recently met an agreement to collect the data that you need in your specific view (indeed, they are assigned by ViewData) to a new class called (NameOfView) ViewModel .

Collecting this data so that it is associated with the functions provided by the View / Controller interaction hit me as an auxiliary structure or even a closing mechanism (in “encapsulates the meaning of a set of variables”).

So why is it called "ViewModel", given that it is neither a view nor a model?

Does anyone else find this name confusing?

EDIT : what's wrong with just placing the properties in the view so that the controller can populate them (as in other MVC environments)?

+7
asp.net-mvc viewmodel
source share
5 answers

A model is an agnostic representation of data. A view model is a presentation of data specific to the view: it model , as it might seem from a given viewpoint.

Consider a model consisting of data source points; the histogram presentation may then have a presentation model consisting of a set of buckets and totals derived from this data.

Logically, this is a subset or transformation of the model - it can be generated on demand using a function specific to the presentation, and the model as its only input.

Regarding the properties in the view versus the property package or user object ... I am sure that someone has strong feelings for this, but personally I do not see much difference. You arbitrarily represent the model and transmit it in some way; the exact mechanism does not seem so important.

+10
source share

In my reading on this topic, I came across various arguments as to why the developer wanted or would not want to use ViewModel. Some even argue that a ViewModel should never expose anything more than strings . At the moment, I'm not so extreme in my thoughts. However, I agree that it is not recommended to host Domain / Core objects for presentation. After some initial experience, he feels cleaner to remove this addiction.

While I disagree with everything below, Daniel Ruth makes a good example for the ViewModel :

Most MVC examples show direct use of a model class such as LINQ-to-SQL or the Entity Framework class. Visual Studio wiring for MVC even drives you into this concept with it by default Generating Code “Add View”, which allows you to quickly view views based on a single model class. However, in real applications, you often need more than just one data table to build a page. Some examples around this are by typing secondary data into ViewData, but the best way to do this is to create a “collapse” class to contain properties for everything your opinion would be needed. This has the added benefit of being more strongly typed, supporting intellisense, being verifiable, and determining exactly what you need to view.

Jeff Handley gives a good description of the ViewModel , which he claims can be used in conjunction with MVC.

Edit
Recently, I brought my thinking in line with Jimmy Bogar regarding models of views. After enough pain with each implementation, I tried with one view model for each view, creating a cleaner development experience.

+12
source share

Re: Why the controller cannot fill in properties in representation?

Because the view does not exist at the time of the controller action. The idea of ​​returning an ActionResult from your action is that something later in the processing pipeline will evaluate the result and determine the best course of action (perhaps displaying the view or, possibly, choosing a view to match the request (for example, special presentations made for mobile devices) devices)).

I looked at the messages about choosing the appropriate type of model object here: Input M in MVC Part I , Part II , Part III .

And yes, the term "ViewModel" is now included in fashion, but it is in the spirit that the original adopters of MVC had in mind.

+2
source share

This is actually not an answer, but I highly recommend you watch the MVC2 Basics video from Scott Hanselman. It explains everything, and even though I did ASP.NET MVC before I could make much of it clear to me.

Here: http://channel9.msdn.com/Blogs/matthijs/ASPNET-MVC-2-Basics-Introduction-by-Scott-Hanselman

+1
source share

He called it because it is "A Model Made for Presentation." I understand why the choice of term is a bit confusing.

This is a useful approach if you do not want all your data to be transmitted as a large array of hashes. It gives a strongly typed class dedicated to the user interface, which does not pollute the base model or view. It also allows you to encapsulate the user interface logic - views must be silent .

0
source share

All Articles