In this Video, I am going to discuss ViewModel in ASP.NET MVC Application with an example. Please watch our previous Video where we discussed Strongly Typed Views in the ASP.NET MVC application. At the end of this Video, you will understand what exactly ViewModel is in ASP.NET MVC and when and how how to use ViewModel in ASP.NET MVC Application.
What is ViewModel in ASP.NET MVC Application?. The Complete ASP.NET MVC Developer Course 2022 [Videos].
What is a ViewModel in ASP.NET MVC?
In an ASP.NET MVC application, a single model object may not contain all the necessary data required for a view. For example, a view may require different model data. Then in such situations like this, we need to use the concept ViewModel. A ViewModel in ASP.NET MVC application is a model which contains more than one model data required for a particular view. As this model is specific for a particular view, we call this ViewModel in ASP.NET MVC.
Understanding ViewModel in ASP.NET MVC:
Let us have a look at the following diagram which shows the visual representation of a ViewModel in the MVC application.
Let say we want to display the employee details on a webpage. And in our application, we have two different models to represent the employee data. The Employee Model is used to represent the basic details of an employee whereas the Employee Address model is used to represent the employee address.
Along with the above two models to represent the employee data, we also required some static information like page header and title in the view. In order to achieve this, here we need to create a view model such as EmployeeDetailsViewModel. It is this view model which is going to contain both Employee and Employee Address models as well as properties to store the title and header of the web page.
Creating the Required Models:
First, create a class file with the name Employee.cs within the Models folder. The Employee model is going to represent the basic information such as name, gender, department, etc. Once you create the Employee.cs class file, then copy and paste the below code in it.
Now, we need to create the Address model to represent the employee Address such as City, State, Country, etc. So, create a class file with the name Address.cs within the Models folder and then copy and paste the following code in it.
Creating the ViewModel in ASP.NET MVC:
Now its time to create the required View Model to store the required data which is required for a particular view. The View Model that we are going to create will represent the Employee Model + Employee Address Model + Some additional properties like title and page header.
In the ASP.NET MVC application, you can create the View Models anywhere within your project, but it is always a good programming practice to create all the View Models within a special folder called ViewModels.
So first create a folder with the name ViewModels and then create a class file with the name EmployeeDetailsViewModel.cs within the ViewModels folder. Then copy and paste the following code into it.
Here we created the view model class with the name as EmployeeDetailsViewModel. Here the word Employee represents the Controller name, the word Details represent the action method name. As it is a view model so we prefixed the word ViewModel. Although it is not mandatory to follow this naming convention, I personally feel it is good to follow this naming convention.
Creating Employee Controller:
Right-click on the Controllers folder and then add a new MVC 5 Empty controller with the name EmployeeController.cs and then copy and paste the following code in it.
As you can see in the above code, here we are passing the employee details view model as a parameter to the view. And one more thing you need to notice is that now we are not using any ViewData or ViewBag within our Details action method.
Creating the Details View:
First, add a folder with the name Employee within the Views folder of your application. Once you add the Employee Folder, then you need to add a view file with the name Details.cshtml within the Employee folder and then copy and paste the following code in it.
Now, the Details view has access to the EmployeeDetailsViewModel object. By using the @model directive, we set EmployeeDetailsViewModel as the Model for our Details view. Then we access Employee, Address, PageTitle, and PageHeader properties using the @Model property.
Now run the application, and navigate to the “/Employee/Details” URL and you will see the output as expected as shown in the below image.