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 bu…


This content originally appeared on DEV Community and was authored by Anshu Verma

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.

Image description

Steps to add ViewModel :-

  1. 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)
  2. Add a View Model class by using the suffix "ViewModel" in the folder "ViewModels".

Image description

  1. Initialize viewModel object in the controller.

Image description

  1. Use the ViewModel object in the view.

Image description


This content originally appeared on DEV Community and was authored by Anshu Verma


Print Share Comment Cite Upload Translate Updates
APA

Anshu Verma | Sciencx (2024-07-17T05:51:51+00:00) What is View Model?. Retrieved from https://www.scien.cx/2024/07/17/what-is-view-model/

MLA
" » What is View Model?." Anshu Verma | Sciencx - Wednesday July 17, 2024, https://www.scien.cx/2024/07/17/what-is-view-model/
HARVARD
Anshu Verma | Sciencx Wednesday July 17, 2024 » What is View Model?., viewed ,<https://www.scien.cx/2024/07/17/what-is-view-model/>
VANCOUVER
Anshu Verma | Sciencx - » What is View Model?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/17/what-is-view-model/
CHICAGO
" » What is View Model?." Anshu Verma | Sciencx - Accessed . https://www.scien.cx/2024/07/17/what-is-view-model/
IEEE
" » What is View Model?." Anshu Verma | Sciencx [Online]. Available: https://www.scien.cx/2024/07/17/what-is-view-model/. [Accessed: ]
rf:citation
» What is View Model? | Anshu Verma | Sciencx | https://www.scien.cx/2024/07/17/what-is-view-model/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.