?

Log in

Previous Entry | Next Entry

A view model  is a simple class which represents data to be displayed on the view.
For example below is a simple customer model object with “CustomerName” and “Amount” property.

CustomerViewModel obj = new CustomerViewModel();
obj.Customer.CustomerName = "Shiv";
obj.Customer.Amount = 1000;


But when this “Customer” model object is displayed on the MVC view it looks something as shown in the below figure. It has “CustomerName” , “Amount”  plus “Customer Buying Level” fields on the view / screen. “Customer buying Level” is a color indication which indicates how aggressive the customer is buying.
“Customer buying level” color depends on the value of the “Amount property. If the amount is greater than 2000 then color is red , if amount is greater than 1500 then color is orange  or else the color is yellow.
In other words “Customer buying level” is an extra property which is calculated on the basis of amount.

                shiv
So the Customer viewmodel class has three properties

  • “TxtCustomerName” textbox takes data from “CustomerName” property as it is.

  • “TxtAmount” textbox takes data from “Amount” property of model as it is.

  • “CustomerBuyingLevelColor” displays color value depending on the “Amount “ value.

shiv2

What kind of logic view model class will have ?

As the name says view model this class has the gel code or connection code which connects the view and the model.

So the view model class can have following kind of logics:-

  • Color transformation logic: - For example you have a “Grade” property in model and you would like your UI to display “red” color for high level grade, “yellow” color  for low level grade and “green” color of ok grade.

  • Data format transformation logic :- Your model has a property “Status” with “Married” and “Unmarried” value. In the UI you would like to display it as a checkbox which is checked if “married” and unchecked if “unmarried”.

  • Aggregation logic: - You have two different Customer and Address model classes and you have view which displays both “Customer” and “Address” data on one go.

  • Structure downsizing: - You have “Customer” model with “customerCode” and “CustomerName” and you want to display just “CustomerName”. So you can create a wrapper around model and expose the necessary properties.

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

January 2017
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Tags

Powered by LiveJournal.com