Passing multiple models to a view using Tuples in an ASP.Net MVC 5.0 application

This is the second post in a series of posts regarding passing multiple models in a view.You can find the first one here

We do know that in any MVC application we cannot pass multiple models from a controller to a single view.

In this post I will provide you with a workaround for passing multiple models in a single view in MVC.

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.

A Tuple object is an immutable, fixed-size and ordered sequence object. It is a data structure that has a specific number and sequence of elements. The .NET framework supports tuples up to seven elements.

Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming.

I am going to use EF as my data access layer. More specifically I will use the Database First approach in EF.

Entity Framework is an object-relational mapping (ORM) framework for the .NET Framework.

EF addresses the problem of Object-relational impedance mismatch. I will not be talking about that mismatch because it is well documented in many sites on the Internet.

Through that framework we can program against a conceptual application model instead of programming directly against a relational schema-model.

1) I will create an empty ASP.Net Application (Empty MVC applicatin) and I give it the name .

I am using Visual Studio 2015 Enterprise edition, C# 5.0 and EF 5.0 version.

2) I will use the AdventureWorksLT2012 database. You can download it by visiting this link.

I have installed SQL Server 2014 Enterprise edition in my machine. SQL Express edition will work fine.

4) I will add an ADO.Net Entity data model using Database First. Follow the wizzard steps, create the connection string and then import into the conceptual model the ProductCategory and Product tables which will become two new entities in the domain model. 

5) Add a new controller class in the Controllers Folder. Name it ProductController.cs

The code for the Controller follows

public class ProductController : Controller
{
// GET: Product


AdventureWorksLT2012Entities ctx = new AdventureWorksLT2012Entities();


public ActionResult Index()
{

var model = new Tuple<List<ProductCategory>, List<Product>>(GetCategory(), GetProducts());
return View(model);

}


public List<Product> GetProducts()
{

List<Product> products = new List<Product>();

var query = from p in ctx.Products
select p;

return query.ToList();
}

public List<ProductCategory> GetCategory()

{


List<ProductCategory> categories = new List<ProductCategory>();


var query = from c in ctx.ProductCategories
select c;

return query.ToList();


}
}

We create an object of the AdventureWorksLT2012Entities class.

We create two methods GetCategory(), GetProducts() that return a list of categories and product objects.

Inside the Index method of the controller 

var model = new Tuple<List<ProductCategory>, List<Product>>(GetCategory(), GetProducts());

we store in the model that we will pass in our view, inside the Tuple object a sequence of ProductCategory,Product objects.

6) Now we need to add a View in our application. Add a view, do not use scaffolding, and name it Index. Place the Index view in the Product folder inside the Views folder.

In the Index.cshtml we have the following code


@model Tuple<List<ProductCategory>, List<Product>>
@{
Layout = null;
}


<b>Categories List</b>
<ul>

@foreach (var item in Model.Item1)
{
<li>@item.Name</li>
}
</ul>

<hr />

<b>Product List</b>

<ul>
@foreach (var item in Model.Item2)
{
<li>@item.Name - @item.Color</li>
}
</ul>

The model we pass to the view is @model Tuple<List<ProductCategory>, List<Product>>

Then we just create two foreach statements and iterate through the list of items. We get the name of the each category and the product name-color from each product in the database.

7) Build and run your application. You will see data from ProductCategory and Product entities. 

Hope it helps!!!

No Comments