Return Multiple Models to View In MVC

Sometimes we need to display two different model's data on one single page, we can very easily manage this with MVC structure. In this article we will learn how to send multiple models to view, and how to use Tuple.

Suppose we have two tables in our model.

First table is UserProfile:

tb2.JPG

Second table is Employee:

tb1.JPG

Suppose we have to show these two different models to one single view; we create one more class that will contain both these classes,

tb3.JPG

We have two ways to display these models together on one view using MVC structure.

  1. By using Merge class

Create another class that will hold Userprofile and Employee, now write the below code to controller that will return MergeModel class to view.

scr4.JPG

Here we are returning multiple employees, so we need to use foreach loop in order to display multiple data from list , write the below code on your view,

scr5.JPG

By using Tuple

By Using Tuples we can include two or more than two models in a single view.

class='lazy' data-src6.JPG


scr7.JPG

Result :

scrn1.JPG