ItemCommentsController – It will retrieve Items and Comments from models. I have added some items and comments in controller class in order to reduce complexity. Using the same method you can display database value to view pages.

What is Session in ASP.NET MVC with Example

ASP.NET Session is also used for passing data between models/controllers to the view page but its value doesn’t become null after passing information until you manually set it to null or session times expires. By default, in ASP.NET MVC, session time is 20 minutes and you can increase or decrease time upon your needs. A session is mostly used in login page where a user is considered as validated until the session expires or the user clicks on log out button.

Step 1: Go to ItemCommentsController.cs and add the following highlighted action method.

Tuples - What are Tuples in ASP.NET MVC with Example

Tuples are first introduced in .NET Framework 4.0. It is immutable, fixed-size and ordered sequence object. It is a data structure that has a specific number and sequence of elements and it supports up to 7 to 8 elements. You can use Tuples for passing multiple models in a single view page.

As we discussed earlier that tuples supports maximum of 7 to 8 items; so pass the models in Tuple<model1, model2, model3,….model7> brackets and then pass the respective action method in sequence that is mapped with model, like (function1, function2, function3,….function7). Here, I did the same thing. Just look this code below. Here I have passed my models and their respective action method in sequence.

Let me highlight some point of this code.1. Add tuples in the beginning of the view page and pass models name in parameter.

@model Tuple<ItemModel, List<BuyersCommentsModel>>

2. You don’t need to remember the models name when accessing its member. You need to use@Model.Item1, @Model.Item2, @Model.Item3…. @Model.Item7 for accessing the model. Here, @Model.Item1 represents ItemModel, and @Model.Item2 represents List<BuyersCommentsModel>http://localhost:1233/ItemComments/ItemCommentDisplay

Dynamic (ExpandoObject) - Example

ExpandoObject can also be used for passing data among Models, Controllers and Views. The member of ExpandoObject can be added or removed at runtime. It is very flexible and dynamic in nature and very useful when you want to display view page dynamically at runtime. We will discuss on Dynamic (ExpandoObject) in details later. Here, just see how to use Dynamic (ExpandoObject) for passing information in MVC.

Summary

In this chapter, you learned how to pass multiple model data to views using Session, Tuples and Dynamic (ExpandoObject) in ASP.NET MVC. I tried to teach you these objects with the help of an easy and complete example. In the next chapter, you will learn how to pass multiple model data to a single view page using Render Action, JSON, and Navigation Property.