7

I'm an ASP.NET MVC newbie, but have used many Model-View-Controller frameworks previously.

I recently came across the convention of gathering up the pieces of data that your particular view needs (indeed, it's assigned to the ViewData) into a new class called (NameOfView)ViewModel.

Gathering up this data so that it's associated with the functions provided by the View/Controller interaction strikes me as a helper struct, or even closure mechanism (in the 'encapsulates a collection of variables' sense).

So why is it called 'ViewModel', given that it's neither a View or Model?

Does anyone else find the name confusing?

EDIT: What's wrong with just putting properties onto the View so that the Controller can populate them (as in other MVC frameworks)?

1
  • 1
    +1 so your rep would no longer be the number of the beast
    – iandisme
    Jan 19, 2010 at 18:40

5 Answers 5

11

In my reading on this topic I've come across a variety of arguments as to why a developer would or would not want to use a ViewModel. Some even argue that a ViewModel should never expose anything more than strings. At this point I am not that extreme in my thinking. I will agree however, that it is not a good idea to expose Domain/Core Objects to the view. After some first hand experience it feels cleaner to remove this dependency.

While I don't agree with everything below Daniel Root makes a pretty good case for a ViewModel:

Most MVC examples show directly using a model class, such as a LINQ-to-SQL or Entity Framework class. The Visual Studio wiring for MVC even steers you into this concept with it's default "Add View" code-generation, which lets you quickly gen up views based on a single model class. However, in real-world-apps you often need more than just a single table's data to build out a page. Some examples get around this by stuffing secondary data into ViewData, but a better way to do this is to create a "roll-up" class to contain properties for everything your view will need. This has the added benefits of being more strongly-typed, supporting intellisense, being testable, and defining exactly what a view needs.

Jeff Handley gives a nice description of the ViewModel pattern which he argues can be used in conjunction with MVC.

Edit
I've recently brought my thinking in line with Jimmy Bogard's regarding view models. After a fair amount of pain with each implementation I've tried having one view model per view creates a much cleaner development experience.

10

The model is a view-agnostic representation of the data. The view model is a view-specific representation of the data: it's the model as it might appear from a given viewpoint.

Consider a model that consists of raw data points; a histogram view might then have a view model consisting of a set of buckets and totals drawn from that data.

Logically, it's a subset or transformation of the model - it could be generated on-demand with a view-specific function and the model as its only input.

Regarding properties on the view vs. a property bag or custom object... I'm sure someone has strong feelings on this, but personally I don't see the big difference. You're producing a view-specific representation of the model and passing it somehow; the exact mechanism doesn't seem all that important.

6
  • 1
    "Logically, it's a subset or transformation of the model " or an amalgamation of multiple models Jan 19, 2010 at 18:41
  • Is this an ASP.NET MVC convention or is it industry-wide? Jan 19, 2010 at 18:42
  • 2
    I don't believe there's anything ASP.NET specific about it... A Google search turns up a lot of WPF references though. It's a pattern; you could conceivably use it with anything.
    – Shog9
    Jan 19, 2010 at 18:44
  • 3
    The term view model seems to be rather new, but the concept of creating a model specific to a view is not. Jan 19, 2010 at 20:29
  • 1
    Another way of looking at it, the "viewmodel" is conceptual standpoint driven by the view, as opposed to the "model" which is a real-state standpoint driven by the data-layer. Perhaps the term "model" should be recoined as "datamodel".
    – sksallaj
    May 8, 2013 at 19:03
1

Re: why can't the controller populate properties on the view?

Because the view doesn't exist at the time the controller action is executing. The idea behind 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 rendering a view, or maybe selecting a view to match the request (like special views made for mobile devices)).

I did a view posts on selecting the right kind of model object here: Putting the M in MVC Part I, Part II, Part III.

And yes, the term "ViewModel" term is en vogue right now, but it is in the spirit the original MVC adopters had in mind.

1

This isn't actually an answer but I highly recommend you watch the MVC2 Basics video by Scott Hanselman. It explains everything, and even though I have done ASP.NET MVC before it made a lot of things clear for me.

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

0

It's called that because it's a "Model made for a View". I understand why the choice of term is a bit confusing.

It's a helpful approach if you don't want all your data passed to the view as a big hash array. It gives you a strongly-typed class dedicated to the UI, which pollutes neither the core Model or the View. It also allows you to encapsulate UI logic -- views should be kept dumb.

1
  • 'views should be kept dumb' - of course, that's why we have Controllers. The article referenced uses constructs like 'if (ViewModel.MeaningfulFlag) { do this } else {do that};' in the view to avoid 'putting business logic in the view'. How is this 'better' than the controller setting an equivalent property on the actual View? Jan 20, 2010 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.