ASP.NET MVC vs. ViewModel

Ok, I heard a discussion of "ViewModels" regarding MS ASP.NET MVC.

Now, what is meant for a particular model, right? Undefined view type.

As far as I understand, this is a kind of model that has a specific purpose of interacting with View? Or something like that?

Some clarifications would be appreciated.

+68
asp.net-mvc viewmodel asp.net-mvc-2 model
Oct 31 '10 at 1:21
source share
5 answers

Essentially, the model and presentation model are simple classes with attributes.

The main purpose of these classes is to describe (for "Model") an object for its respective audiences, which are respectively the controller and presentation.

So you are absolutely right when you say

As far as I understand, this is a kind of Model that has a specific goal interacting with the view

So, while model classes are actually domain objects that your application interacts with, view models are simple classes that your views interact with.

Hope this helps :)

Update

Microsoft has developed a specialized version of the Martin Fowler presentation template based primarily on Model-View-Controller and named it Model-View-ViewModel (MVVM) for the PF application. This template focuses on modern user interface development platforms, where user interface developers have different requirements based more on business logic than traditional developers. Look here for a little theory.

+52
Oct 31 '10 at 1:32
source share

In the simplest of terms, I like to think about the following:

Model: Strictly looks and looks like your data model. In every sense and purpose, this is just a representation of the class of your data model. He does not know your representation or any elements in your representation. However, it should not contain attribute decorators (i.e. Required, Length, etc.) that you would use for your presentation.

View Model: Serves as a link between your view and your model, and in many cases is also a wrapper for your model. This will be useless without presentation, so it usually cannot be reused for multiple views and controllers, such as the standard model.

As an example, your model can have the following properties, which are direct representations of your data source:

public string FirstName { get; set; } public string LastName { get; set; } 

Now, since your View model is tied to your view, it can have the following property - which combines the Model FirstName field and the LastName field together as one line:

  [Display(Name = "Customer Name")] public string CustomerFullName { get { return String.Format("{0} {1}", myModel.FirstName, myModel.LastName) }} 
+48
Sep 14 '13 at 23:24
source share

I found this article a very useful resource for understanding how the “domain model” and “view model” interact in an MVC application, especially with respect to binding. It is best to include examples instead of abstract descriptions.

“Since MVC was released, I observed a lot of misunderstanding about how to best create models of views. Sometimes this confusion is not without reason, because there seems to be no ton of information in the best practice recommendations. In addition, there is no“ one size fits ”solution to everyone, "which acts like a silver bullet. In this post I describe several basic patterns that arose, and the pros / cons of each. It is important to note that many of these patterns arose in people who solve real problems."

http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

+24
May 03 '11 at 1:50
source share

WikiPedia has a more complete description of Model vs. ModelView, what will you get in response to SO: http://en.wikipedia.org/wiki/Model_View_ViewModel

I quote:

Model : as in the classic MVC template, the model refers to either (a) an object model that represents real state content (an object-oriented approach), or (b) data is the access level that this content represents (a data-oriented approach).

View : as in the classic MVC template, the view applies to all elements displayed by the graphical interface, such as buttons, windows, graphics, and other controls.

ViewModel : ViewModel is the "View Model", which means the abstraction of the view, which also serves to bind data between the view and the model. This can be seen as a specialized aspect of what will be the Controller (in the MVC template), which acts as a binder / data converter that changes the model information into a presentation of information and transfers commands from the view to the model. ViewModel provides public properties, commands, and abstractions. The ViewModel is compared with the conceptual state of the data, and not with the actual state of the data in the Model.

+16
Oct. 31 '10 at 1:47 a.m.
source share

There is a concept called ViewModel, but it is usually not associated with Asp.net MVC. MVC uses the model view control panel, where the controller processes the interactions, creates data from the model, and then transfers this data to the view for display.

ViewModels (and the Model View ViewModel template) are more associated with Silverlight and WPF. Xaml is slightly different in that views can do two-way binding to ViewModels, so the technology is slightly different. For example, if you bind a text field to a field, as you type into this text field, the value of the field is updated dynamically. This kind of interaction is actually not possible in web pages because web pages have no status.

The similarity in the two patterns is that they both try to separate the logic from the display. The most common use / reason for this is testing: you want to be able to execute from the code (through the testing structure) all the interactions that the user will invoke through the user interface.

+5
Oct 31 '10 at 1:48
source share



All Articles