Can you explain the purpose of ViewModel in MVC?( ASP.NET MVC Interview questions with answers)

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.

view

Let’s talk about two scenario where ViewModel will be preferred over Model

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


Also one of colleagues MrSukeshmarla(
SukeshMarla@Gmail.com)takes live MVC trainings on various topics including MVC, WCF, Design patterns, WPF, BI etc.please visit www.sukesh-Marla.com

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s