For ASP.NET MVC beginner, it is very common problems that they faces in programming world: That how they can use multiple Models in their ASP.NET MVC application’s view. Some of beginners is familiar with some of way to use multiple Models in an ASP.NET MVC application. But, May be they are not know about all the way to use Multiple Models in ASP.NET MVC. Here, in this article I am going to share all the possible ways to use multiple Models in a view. I am assuming that you will familiar with C# and ASP.NET MVC. If you are new to C# and ASP.NET MVC, you can look for different articles to learn ASP.NET MVC. Still, if you will face any issue you can put comment below article in case you need any detail.

There are several ways to use multiple models in a view in ASP.NET MVC application, Following are the list:

ViewModel

PartialView

ViewData

ViewBag

TempData

Tuple

Let’s start this article, we have following Models, which we will use to show data at our View.

For only demo purpose, we will create some sample data, which is hard coded in a class file under the Models folder named as Data.cs file, which will have the implementation of methods to get hardcoded data for application in order to keep it convenient.

Following is the code for that we have to get all the temporary Data from Data.cs file.

Now, we have a view in Views/Home folder named Employee.cshtml, to show the information of employee, we are create links at index.cshtml file to access the employee list. Each links will access by one of a way to use multiple Models. Here are the sample code.

So far, we have created basic code which we will be using in all scenarios. Further, we will learn each scenario one by one.

Using Multiple Models at View using ViewModel

ViewModel is a pattern that can be used to have multiple models as a single class. It contains properties of entities exactly need to be used in a view.

We have three models (classes) named as Employee, Course and Department. All are different models but we need to use all three models in a view. We will create a ViewModel by adding a new folder called ViewModels and in class file called EmployeeModels.cs and write the following code as shown below:

ViewModelDemo is an action method that will return a view with Model ViewModelDemo which has lists of allCourses, Employees and Departments. We need to add a View to Show these data, to do that: Right click on ViewModelDemo to add a view is called ViewModelDemo. ViewModelDemo.cshtml view will use ViewModelDemo as Model as shown below: this code is written in ViewModelDemo.cshtml

As, we have defined two new PartialView in above Action Methods, So, we need to create these Partial View in to the Shared folder by right clicking on DepartmentPartialDemo action method, give it name as DepartmentPartialView. And similarly, for CoursePartialDemo Action Method create another partial View CoursePartialView. Following, are the sample code of both partial views:

Using Multiple Models at View using ViewData

The very important features and use of ViewData is used to pass data from a controller to a view. We can be define ViewData is a dictionary of objects that is a type of ViewDataDictionary class. In ASP.NET MVC, ViewData is defined as property in ControllerBase class. To Use the values stored in ViewData, It is require typecasting to their datatype in view. The values in ViewData are accessible using a key. Let’s see in details how can we use ViewData to pass multiple Models in a View in ASP.NET MVC.

First, we will create a new Action in HomeController named ViewDataDemo, Following are the sample code.

Using Multiple Models at View using ViewBag

Like ViewData, ViewBag is also used to pass data from a controller to a view. It is a dynamic property which comes in ControllerBase class that is why it doesn’t require typecasting for datatype. To call multiple Models using ViewBag Create an Action Method ViewBagDemo in HomeController, Following are the sample code:

Using Multiple Models at View using TempData

Like ViewData, TempData is a dictionary of objects that is a type of TempDataDictionary class. The data is stored temporary, So we need to call Keep method to keep the data in TempData. . TempData is similar to ViewData but the difference is that it allow us to send and receive the data from one controller to another controller and from one action to another action. It’s all about possible because it internally uses session variables. TempData is defined as property in ControllerBase class. Values stored in TempData require typecasting to datatype in view. The values in TempData are accessible using a key.

To use Multiple Models using TempData, we will create an Action in HomeController named TempDataDemo. Following are the sample code:

public ActionResult TempDataDemo()
{
Data _dummyData = new Data();
// TempData demo uses dummyData to get List<Employees> only one time
// for subsequent request to get List<Employees> it will use TempData
TempData["Employees"] = _dummyData.GetAllEmployee();
// This will keep Employees data untill next request is served
TempData.Keep("Employees");
TempData["Departments"] = _dummyData.GetAllDepartment();
TempData.Keep("Departments");
TempData["Courses"] = _dummyData.GetAllCourse();
TempData.Keep("Courses");
return View();
}

Now, we will create a View for this Action named TempDataDemo.cshtml. Following are the sample code to use Multiple Models at View via TempData:

Using Multiple Models in View using Tuple

A tuple is a data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) that is used to store an identifier such as a person's name in the first element, a year in the second element, and the person's income for that year in the third element. The .NET Framework directly supports tuples with one to seven elements. In addition, you can create tuples of eight or more elements by nesting tuple objects in the Rest property of a Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object.

Passing multiple Models in View using Tuple is very easy, to do this let’s create an Action in HomeController. Following are sample code: