What is View Model?

What is View Model?

If we need to pass two different models to a View e.g. one is the movie and other is the list of the customers but here we have only one model property in the view so how do we solve this problem? We use a View Model. View Model is a Model specially built for a view. It includes any data and roles specific to that view.

Steps to add ViewModel :-

Add new folder “ViewModels” in the Solution Explorer. (We use the Models folder purely for our domain classes like Movie, Customer and so on. We put View Models in a separate folder)
Add a View Model class by using the suffix “ViewModel” in the folder “ViewModels”.

Initialize viewModel object in the controller.

Use the ViewModel object in the view.

Please follow and like us:
Pin Share