Do not forget to see these 40 MVC interview questions and answer list from http://www.codeproject.com/Articles/556995/MVC-interview-questions-with-answers
Many people feel this question as very strange because MVC stands for Model, View and Controller, then what’s this new View Model means here.
So the most obvious answer we hear is “In MVC we don’t have VIewModel”. This is not the correct answer.
In MVC Model is termed as Domain Modal whereas ViewModal is nothing but presentation Modal.
ViewModal will be more specific to rendering of view. It looks like Modal, a simple class containing properties (properties more specific to display). So instead of creating strongly typed view of Model we will create strongly typed view of ViewModel.
Let’s talk about two scenario where ViewModel will be preferred over Model
- Our business requirement containDateOfBirth as one field and so our Model contain a property called DateOfBirth. Now our requirement is to show Age on the view. Polluting our Model with unnecessary properties will not be a good idea. So the best solution will be
a. create a replica of our Model class (call it View Model)
b. add an extra property called age into new class
c. And bind the view with this newly created class
Some more similar situations are
a. Display money values formatted in some way
b. Show some datetime values in some specific format
- We want to make our view strongly typed but of more than one modal.
Example let say we have a view called ProductDetail.aspx and it should be strongly typed with both Products and Customer. It’s not possible unless and until we use ViewData or ViewBag which itself bring new problems.
So the ultimate solution will be create a new class called ViewModel with two properties one of Customer type and one of Product type and this class will be used for creating strongly typed view.
With the introduction of view model in MVC separation of concerns is brought off into even higher level. Controller contains user interaction logic, View contains UI/Design, ViewModel contain Presentation logic and Model contains Business Logic.
Recently one of readers was asked about MVC async controllers in MVC interview below is a video created by www.questpond.com which demonstrates MVC async and thread starvation.