DEV Community

Anshu Verma
Anshu Verma

Posted on

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.

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay