What is Model and ViewModel in MVC Pattern?

Model and ViewModel are two things we always hear about in MVC. In this article I will show you the differences between them.

Let's begin with its common definition.

What is Model or Domain Model?


Actually, the word "model" has hundreds of meanings in software development, but here we will talk about "model" in the context of a MVC design pattern. I would define a model as an object that we use to send information to the database, to perform business calculations and to render a view. In other words, "model" represents the domain of the application that helps us to save, create, update and delete records. Usually we put all our model classes in the Model folder.

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

Let's assume we want to implement a view page that will have three textboxes for Username, Password and Re-enter Password. To do this we could design a "Model" as given below:

  1. public class Login  
  2. {  
  3.     public String Username { getset; }  
  4.     public String Password { getset; }  
  5.     public String RePassword { getset; }  
  6. }
For the sake of the view this model works fine. But this is actually an incorrect approach because we are trying to overcrowd the database. I can't see any use of the "RePassword" property in the database.

Now, if we take the advantage of a ViewModel, we can safeguard the database from redundant data. Here's how, design the following "Model" that will be our Domain Model:

 

  1. //this will represent domain of the application  
  2. public class Login  
  3. {  
  4.     public String Username { getset; }  
  5.     public String Password { getset; }  
  6. }  
And then following "ViewModel"
  1. //this will help in rendering great views  
  2. public class LoginViewModel  
  3. {  
  4.     public String Username { getset; }  
  5.     public String Password { getset; }  
  6.     public String RePassword { getset; }  
  7. }

Now, when adding the view, pick the ViewModel class to get the strongly-typed benefits.

viewmodel.png

Now the question is, how do we transform the Model or Domain Model from the "ViewModel"? Let's see how.

Transforming Model from ViewModel


There are various ways to do this. In the form POST action we can create a new object of type Login model and then assign the properties one by one and leave the unwanted properties.

  1. [HttpPost]  
  2. public ActionResult Login(LoginViewModel viewModel)  
  3. {  
  4.     //validate the ViewModel  
  5.     //transforming Model (Domain Model) which is Login from ViewModel which is LoginViewModel  
  6.     var login = new Login()  
  7.     {  
  8.         Username = viewModel.Username,  
  9.         Password = viewModel.Password  
  10.     };  
  11.     //save the login var  
  12.     return View();  
  13. }

In the code above, after validating the ViewModel I'm transforming the Model or Domain Model. So, by using this way you can stop overcrowding the database with unnecessary fields.

Let's look at one more use of ViewModel

I've a blog where I want to display the list of the latest posts, latest comments, post categories in a single view. How I can do that? I can strongly type my view to any one of the models, right? Here comes the view model.

I created a view model called BlogViewModel that contains the latest posts, latest comments and other stuff as properties and I bind my view with this model. The posts and comments are domain models while the BlogViewModel is the view model I created specially for the view.

Tomorrow I'll show my blog in a mobile version and at that time I may create a simple view model that contains only fewer properties. Finally, view models are for views and usually they acts as wrappers over the real domain models.

Hope this helps.


Similar Articles