Unable to understand the difference between ViewModels and Models

I am working on ASP.net MVC. Here M supports only the "Model" . A model that can be mapped to a database.

I usually bind my View strongly to the model.

But lately I've heard about the View Model, something different from the model.

Is the ViewModel part of MVC or MVVM (ModelViewViewModel)?

What is the main difference between ViewModel and Model. I would like to give a small example of how I work in MVC:

My view:

 <% using (Ajax.BeginForm("TestAjax", "Reviewer", new AjaxOptions { OnSuccess = "Callback", OnFailure = "Failed" })) { %> <table align="center"> <tr> <td class="tdCol1Align"> <label> Number1</label> </td> <td class="tdCol2Align"> <%=Html.TextBoxFor(Model => Model.number1)%> </td> </tr> <tr> <td class="tdCol1Align"> <label> Number2</label> </td> <td class="tdCol2Align"> <%=Html.TextBoxFor(Model => Model.number2)%> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Add" class="button" /> </td> </tr> </table> <% } %> 

My model:

 public class AddModel { public int number1 { get; set; } public int number2 { get; set; } } 

and finally My controller :

  [HttpPost] public JsonResult TestAjax(AddModel model) { int iSum = model.number1 + model.number2; return Json(new { Sum = iSum }); } 

What is it. I can’t understand where this ViewModel goes into Image

Please clarify the following:

1. The main difference between ViewModel and Model

2.Is View Model Part MVC or MVVM Architecture?

  • How to implement the above example using ViewModels?

  • If view models are part of MVC, then where will they be in the folder structure of the application?

+1
c # asp.net-mvc viewmodel model
Oct 25 '13 at 11:27
source share
4 answers

Is the ViewModel part of MVC or MVVM (ModelViewViewModel)?

ViewModel is an M in MVC.

What is the main difference between ViewModel and Model.

A model that can be mapped to a database is not M in MVC. This is a domain or business model. What you need to pass to your views from the controller action is the view model. A single presentation model may be a combination of several domain models.

I can’t figure out where this ViewModel in Picture comes in.

In this example, the view model is an anonymous type that you created here:

 return Json(new { Sum = iSum }); 

This is basically a class with one property called Sum . Perhaps it would be clearer if you defined the actual view model instead of using an anonymous type:

 public class ResultViewModel { public int Sum { get; set; } } 

that the action of your controller could be passed to the view:

 [HttpPost] public JsonResult TestAjax(AddModel model) { int iSum = model.number1 + model.number2; ResultViewModel viewModel = new ResultViewModel(); viewModel.Sum = iSum; return Json(viewModel); } 

but since in this case the actual view other than the JSON result using the anonymous type is just fine.

+4
Oct 25 '13 at 11:31
source share

The MVC view model does not match the MVVM view model.

For MVC, a presentation model is a kind of model that is designed for one or more views. We usually call the “Model” objects that are also used by your business layer and data access layer. But many times they are poorly suited for displaying data to users, since views may have specific needs. Perhaps you need to combine several objects, make some transformations, then create a view model that contains only the necessary data. The model should be displayed in the view with data ready for display, and no additional process should be done.

+1
Oct. 25 '13 at 11:37
source share

A model in MVC is not just a class (POCO) that represents one (or more) tables in a database. This is the layer. It contains all the business logic, validation and data access for your application. It also consists of domain models, which are database tables.

As your application grows and you need more flexibility and complex views, the domain model is usually not suitable for use in the view. Especially when you add validation attributes that are different from validating your database, or when your view contains properties of more than one domain model. For example, to enter the viewing system, you do not want to use the User domain model here, but a specialized presentation model that contains only those properties that are necessary for the presentation:

 public class LoginModel { [Required] public string Username { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } public bool RememberMe { get; set; } } 
+1
Oct 25 '13 at 11:43
source share

A simple explanation is

Model . It is defined to define a property for reading values ​​from a database (as well as for a data access layer)

Show model . It is defined to define properties for reade write values ​​for View (UI) (as well as for business logic)

Edit:

If you use the MVVM template, then you will manually create a folder with the name as ViewModel in the solution explorer. But if you used the MVC pattern, then the model is automatically created in the solution explorer,

The Model folder has classes related to the database (To include some methods for reading and writing data to the database), View Model have our business logic and UI-related classes (to include some properties for reading and writing values ​​with a view ( UI) and View have our html or any page in the MVVM template

The model has database classes and business logic and interface classes, while View have our html or any page in the MVC template

+1
Oct 25 '13 at 11:47
source share



All Articles