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:
Second table is Employee:
Suppose we have to show these two different models to one single view; we create one more class that will contain both these classes,
We have two ways to display these models together on one view using MVC structure.
-
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.
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,
By using Tuple
By Using Tuples we can include two or more than two models in a single view.
Result :