This article will tell you almost everything about ViewModel in ASP.NET MVC. I am writing this article to tell you the basic to advanced concepts about ways to manage the data and organize the code in ASP.NET MVC. Stay tuned for my coming articles.
I would strongly recommend reading my previous articles, which will be defining the basic concepts of ASP.NET MVC, as listed below.
- Passing Data From Controller To View Using Session - Part Five
- Passing Data From Controller To View With TempData - Part Four
- Passing Data From Controller To View With ViewBag - Part Three
- Passing Data From Controller To View With ViewData - Part Two
- Passing Data From Controller To View - Part One
- Action Selectors In ASP.NET MVC
- Filters In ASP.NET MVC
- Action Result In ASP.NET MVC
- MVC Architecture And Its Pipeline
- Convention Routing VS Attribute Routing
Read this article, check the code after downloading, and then feel free to give your feedback.
The topics to be covered are,
- ViewModel and its usage
- How data can be managed with ViewModel
- Where we should use ViewModel
- Understanding ViewModel with a practical example
Introduction and Background
The concept of ViewModels is not only for ASP.NET MVC. It is also used in MVC, MVP, and MVVM design patterns. In this article, you’ll learn what ViewModels are and how we can manage and clean the code with ViewModels.
What ViewModel is?
In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into a single object provides us with better optimization. You can see the concept of ViewModel in the image below.

As you can see, if we want to display more than one Model in a single View, we have to pass a ViewModel to that View, so that we can take the benefits of both models into a single object. So, we have to use ViewModel for better performance of sources.
How data is managed with ViewModel?
ViewModel provides us with proper Separation of Concerns (SoC). The View needs to render only the single ViewModel object, and there is a specific purpose for each and every aspect of the application, which means the application will be more organized in the code. Using ViewModel, we put our manipulation code separately in a specific place and away from the View and Controller.
So, it provides us with a better SoC and makes our code more maintainable. It also makes our code more testable because unit testing means testing the small units, and it is easier to test if we separate the manipulation logic in a separate ViewModel.
Where we should use ViewModel?
We can use ViewModel in the following scenarios,
- Managing or creating dropdown lists for a specific entity
- Creating Master-Details View in data-driven websites
- Used in showing shopping carts on the website
- Used in Pagination
- Used in a website to show a User profile widget
- Used to make Dashboards for integrating multiple data into one place
- Used as an Aggregate Root to make reports in Domain Driven Design (DDD)
These are the complex scenarios in which ViewModel can be used for better maintainability, reliability, and testability of code.
Understanding ViewModel with a practical example
Let’s take an example in which there is a View for showing a list of books. And, we also want to show the list of customers who rented these books. We don’t have any relationship between books and the customers so, the purpose is to only show the data of both classes together. As you know, there will be separate model classes for Book and Customer, and we can’t pass both models into a single View. Hence, we have to make a ViewModel.
Let’s create a project with the name ViewModelsDemo.









Joao ZaraPosted May 17, 2023, 2:42 PM
Very good tutorial approved. However, if we have an empty List, on foreach view it returns the error "System.NullReferenceException: 'Object reference not set to an instance of an object." Any suggestion! Thank you
Ijaz KhanPosted Jun 3, 2020, 1:39 AM
Nice and clean