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

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)