Tuesday, 2 July 2013

ViewModel in mvc3

In asp.net mvc, a controller can pass single object to the corresponding view. This object may be a simple object or a complex object. ViewModel is a complex object that may contain multiple entities or objects from different data models or data source. Basically ViewModel is a class that specify data model used in the strongly-typed view. It is used to pass data from controller to strongly-typed view.


Key Points about View Model
    ViewModel contain fields that are represented in the view (for LabelFor,EditorFor,DisplayFor helpers)
    ViewModel can have specific validation rules using data annotations or IDataErrorInfo.
    ViewModel can have multiple entities or objects from different data models or data source. 

Some Tips for using ViewModel
    In ViewModel put only those fields/data that you want to display on the view/page.
    Since view reperesents the properties of the ViewModel, hence it is easy for rendering and maintenance.
    Use a mapper when ViewModel become more complex. 

ViewModel help us to organize and manage data in a strongly-typed view with more flexible way than complex objects like models or ViewBag/ViewData objects.

Other Links Related to ViewModel are : 
1- what-is-model-and-viewmodel-in-mvc

2 comments:

  1. View model of MVC3 is really a complex object or as a class, this blog contribute helpful lines for mvc3 developer, it is used for good .NET MVC3 Development by xicom technologies.

    ReplyDelete
  2. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    ios App Development Company
    Android App Development Company

    ReplyDelete

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives