1. Back To Blog

QuestPond's interview questions and answers on ViewModel in ASP.NET MVC(Model View Controller)

How to pass two models in one view in ASP.NET MVC(Model View Controller)?


This is one of the most confusing ASP.NET MVC(Model View Controller) interview questions among every beginner learning ASP.NET MVC(Model View Controller).


Normally in MVC(Model View Controller) one view is bound one Model.



In real time projects we always end up into a solution where we want more than one kind of model in a view. Let's understand how to tackle this solution.


Try to answer following questions one after other and then let see if you can understand it or no.


  1. Question: Let say you have been asked to create a simple C# method which accept customer name as argument, what you will do?

    Answer: I will simply create a method with parameter of type string.

  2. Question: Correct, Let say function is going to accept more than one kind of customer attributes. Example Customer_Name, Address etc. What will be your take on it?

    Answer: I will customer class and encapsulate all the attributes inside it and finally use Customer Type as the argument of method

  3. Question: Now let's come to ASP.NET MVC(Model View Controller). We want to create view which is going display Customer data in the screen. It will display Customer Address, Customer name etc. So how can I achieve it?

    Answer: I will create a Model called Customer encapsulating all the customer attributes and then create a strongly typed view out of it.

  4. Question: Now answer this. You have a view where you want to display both customer and department information. What you will do?

    Answer:  I got it I will create a third class which encapsulated both Customer class and Department class. Finally from that third class I will create a strongly typed view.


Example:



This kind of class is called as ViewModel in MVC(Model View Controller).


For technical trainings on various topics like WCF (Windows Communication Foundation), MVC (Model View Controller), Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com


For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social