Sunday, July 17, 2022

What is ViewModel with Example and Uses and benifits.

 What is ViewModel?

In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization. You can see the concept of ViewModel in the image below.





so you can see, if we want to display more than one Model into a single View, we have to pass a ViewModel to that View, so that we can take benefits of both the models into a single object. So, we have to use ViewModel for  better performance of sources.


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 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.

For Coding example please refer the below link

https://www.c-sharpcorner.com/article/managing-data-with-viewmodel-in-asp-net-mvc/



No comments:

Post a Comment