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

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.

There are several approaches to achieve this but I will use the List<object> and the object object.

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 (Index method) follows

public class ProductController : Controller
{
// GET: Product

AdventureWorksLT2012Entities ctx = new AdventureWorksLT2012Entities();


   public ActionResult Index()
   {


         List<object> model = new List<object>();

         model.Add(ctx.ProductCategories.ToList());

         model.Add(ctx.Products.ToList());

        return View(model);
    }
}

We create an object of the AdventureWorksLT2012Entities class.

Then we create a List<object> object list. We add the Products collection and ProductCategories collection to that list.

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 IEnumerable<object>

@{

List<PassingMultipleModelsToAView.ProductCategory> lstPCategory = Model.ToList()[0] as List<PassingMultipleModelsToAView.ProductCategory>;

List<PassingMultipleModelsToAView.Product> lstProduct = Model.ToList()[1] as List<PassingMultipleModelsToAView.Product>;
}

<h3>Categories</h3>

<ul>

@foreach (var item in lstPCategory)
{

<li>@item.Name</li>
}

</ul>


<hr />

<h3>Products</h3>

<ul>

@foreach (var item in lstProduct)
{
<li>@item.Name-@item.Color</li>

}


</ul>

The model we pass to the view is @model IEnumerable<object>.

Then we pass the first list of the model (ProductCategory) to a list of the same type and the second list of the model(Product) to a list of the same type.

Then we just create two foreach statements and iterate through the list of items.

7) Build and run your application. You will see data from ProductCategory and Product entities. So we still passed a model to the view (model IEnumerable<object>) but this model represented two entities/models so in reality we passed 

PassingMultipleModelsToAView.ProductCategory, PassingMultipleModelsToAView.Product models to the view and achieved our goal.

Hope it helps!!!

3 Comments

Comments have been disabled for this content.