Pass Model Data To View Page In ASP.NET MVC 5

This section is very important for you because here you will learn various techniques to transfer model data to view pages. Till now, you have learned how to access database value using models and controllers, and this time is to learn how to display those data to view pages. After completing this section, you will be able to design model data to view pages with style.

There are following 9 ways by which you can display model data to view pages.

1. View Model
2. ViewBag, ViewData, and TempData
3. Session, Tuples, and Dynamic
4. Render Action and Navigation Properties

ViewModel – Display Multiple Model Values Into Views In ASP.NET MVC 5

VIEW MODEL
View Model is nothing, just a class that calls all models in a single class and then returns values to view pages. It becomes very useful when you have multiple models and want to display their value in one view pages.

1. CREATE MODELS AND CONTROLLERS
Before Starting this section, you must create following classes.

 

ItemModels – It retrieves Item Details

  1. Using System;
  2. namespace CompShop.Models
  3. {
  4. public class ItemModel
  5. {
  6. public int ID { get; set; }
  7. public string Name { get; set; }
  8. public string Category { get; set; }
  9. public decimal Price { get; set; }
  10. }
  11. }

BuyersCommentsModel – It retrieves Comments

  1. using System;
  2. namespace CompShop.Models
  3. {
  4. public class BuyersCommentsModel
  5. {
  6. public int BuyersID { get; set; }
  7. public string BuyersName { get; set; }
  8. public string StarRating { get; set; }
  9. public string CommentsTitle { get; set; }
  10. public string Comments { get; set; }
  11. public DateTime Date { get; set; }
  12. }
  13. }
And 1 Controller class:

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.

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.Mvc;
  4. using CompShop.Models;
  5.  
  6. namespace CompShop.Controllers
  7. {
  8. public class ItemCommentsController : Controller
  9. {
  10. // GET: ItemComments
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15.  
  16. public ItemModel GetItemDetails()
  17. {
  18. ItemModel iModel = new ItemModel()
  19. {
  20. ID = 1,
  21. Name = “HP Printer”,
  22. Category = “ComputerParts”,
  23. Price = 8840
  24. };
  25. return iModel;
  26. }
  27.  
  28. public List<BuyersCommentsModel> GetCommentList()
  29. {
  30. List<BuyersCommentsModel> CommentsList = new List<BuyersCommentsModel>();
  31. CommentsList.Add(new BuyersCommentsModel() { BuyersID = 1, BuyersName = “John”, StarRating = “4”, CommentsTitle = “Nice Product”, Comments = “I Purchased it 1 week ago and it is working fine.”, Date = Convert.ToDateTime(“27 August 17”) });
  32. CommentsList.Add(new BuyersCommentsModel() {BuyersID=2, BuyersName=“Nicki”, StarRating=“2”, CommentsTitle=“Worst Product”, Comments=“Worst Product. Don’t Buy It. I got damaged one.”, Date=Convert.ToDateTime(“12 June 17”) });
  33. CommentsList.Add(new BuyersCommentsModel() {BuyersID=3, BuyersName=“Serena”, StarRating=“3.5”, CommentsTitle=“Satisfactory”, Comments=“Go for it. It does the same job and have the less price”, Date=Convert.ToDateTime(“18 March 17”) });
  34. CommentsList.Add(new BuyersCommentsModel() {BuyersID=4, BuyersName=“William”, StarRating=“4.5”, CommentsTitle=“Superrr!!!”, Comments=“Don’t think and buy it with confidence.”, Date=Convert.ToDateTime(“11 November 16”) });
  35.  
  36. return CommentsList;
  37. }
  38.  
  39. public ActionResult ItemCommentDisplay()
  40. {
  41. }
  42. }
  43. }

2. ACCESS MODEL DATA USING VIEWMODEL

  1. using System.Collections.Generic;
  2. namespace CompShop.Models
  3. {
  4. public class ItemCommentsViewModel
  5. {
  6. public ItemModel item { get; set; }
  7. public List<BuyersCommentsModel> comments { get; set; }
  8. }
  9. }

Step 2: Add highlighted (Line: 42-49) code snippets in ItemCommentsController.

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.Mvc;
  4. using CompShop.Models;
  5.  
  6. namespace CompShop.Controllers
  7. {
  8. public class ItemCommentsController : Controller
  9. {
  10. // GET: ItemComments
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15.  
  16. // *** 1. This Method contains dummy Item Details. ***
  17. public ItemModel GetItemDetails()
  18. {
  19. ItemModel iModel = new ItemModel()
  20. {
  21. ID = 1,
  22. Name = “HP Printer”,
  23. Category = “ComputerParts”,
  24. Price = 8840
  25. };
  26. return iModel;
  27. }
  28.  
  29. // *** 2. This Method contains dummy Comment List. ***
  30. public List<BuyersCommentsModel> GetCommentList()
  31. {
  32. List<BuyersCommentsModel> CommentsList = new List<BuyersCommentsModel>();
  33. CommentsList.Add(new BuyersCommentsModel() { BuyersID = 1, BuyersName = “John”, StarRating = “4”, CommentsTitle = “Nice Product”, Comments = “I Purchased it 1 week ago and it is working fine.”, Date = Convert.ToDateTime(“27 August 17”) });
  34. CommentsList.Add(new BuyersCommentsModel() {BuyersID=2, BuyersName=“Nicki”, StarRating=“2”, CommentsTitle=“Worst Product”, Comments=“Worst Product. Don’t Buy It. I got damaged one.”, Date=Convert.ToDateTime(“12 June 17”) });
  35. CommentsList.Add(new BuyersCommentsModel() {BuyersID=3, BuyersName=“Serena”, StarRating=“3.5”, CommentsTitle=“Satisfactory”, Comments=“Go for it. It does the same job and have the less price”, Date=Convert.ToDateTime(“18 March 17”) });
  36. CommentsList.Add(new BuyersCommentsModel() {BuyersID=4, BuyersName=“William”, StarRating=“4.5”, CommentsTitle=“Superrr!!!”, Comments=“Don’t think and buy it with confidence.”, Date=Convert.ToDateTime(“11 November 16”) });
  37.  
  38. return CommentsList;
  39. }
  40.  
  41. // *** 3. This Method retrieves Items and Comments, and returns to Views.
  42. public ActionResult ItemCommentDisplay()
  43. {
  44. ItemCommentsViewModel IBCM = new ItemCommentsViewModel();
  45. IBCM.item = GetItemDetails();
  46. IBCM.comments = GetCommentList();
  47.  
  48. return View(IBCM);
  49. }
  50. }
  51. }

 Step 3: Right-click on ItemCommentDisplay Action Method and select Add View…

 

Step 4: Set the value as this picture below and click Add:

Step 5: Now, go to Views  ItemComments  ItemCommentDisplay.cshtml and Add the following code.

  1. @model CompShop.Models.ItemCommentsViewModel
  2.  
  3. <div>
  4. <h4>Buyers Comments</h4>
  5. <hr />
  6. <dl class=“dl-horizontal”>
  7. </dl>
  8. </div>
  9. <p>
  10. <!– ITEM DETAILS –>
  11. <div style=“background-color:burlywood; padding:5px;”>
  12. <h1 style=“color:green”>@Html.DisplayFor(i => i.item.Name)</h1>
  13. <b>Category:</b> @Html.DisplayFor(i => i.item.Category)<br />
  14. <b>Price:</b> @Html.DisplayFor(i => i.item.Price)
  15.  
  16. <hr />
  17. </div>
  18. <!– LOADING COMMENTS –>
  19. <h2>Comments</h2>
  20. @foreach (var comments in Model.comments)
  21. {
  22. <h4><b>@comments.CommentsTitle</b></h4>
  23. <b>@comments.BuyersName</b><br />
  24. <b>Date:</b> @comments.Date <span>|</span>
  25. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  26. <br />
  27. <i>@comments.Comments</i>
  28. <hr />
  29. }
  30. </p>

Step 6: Press F5 to run this program and navigate to following link to see the output.

 

Pass Data Using ViewBag, ViewData And TempData In ASP.NET MVC 5

In the previous chapter, you learned what is ViewModel and how to use it for sending model data to views. In this chapter, you will learn how to use ViewBag, ViewData and TempData for passing information from controllers/model to views.

 

CREATE MODELS AND CONTROLLERS

Before Starting this section, you must create following model classes and controllers. If you have already created it in the previous chapter then skip creating these models and controllers.

ItemModels – It retrieves Item Details

  1. Using System;
  2. namespace CompShop.Models
  3. {
  4. public class ItemModel
  5. {
  6. public int ID { get; set; }
  7. public string Name { get; set; }
  8. public string Category { get; set; }
  9. public decimal Price { get; set; }
  10. }
  11. }

BuyersCommentsModel – It retrieves Comments

  1. using System;
  2. namespace CompShop.Models
  3. {
  4. public class BuyersCommentsModel
  5. {
  6. public int BuyersID { get; set; }
  7. public string BuyersName { get; set; }
  8. public string StarRating { get; set; }
  9. public string CommentsTitle { get; set; }
  10. public string Comments { get; set; }
  11. public DateTime Date { get; set; }
  12. }
  13. }
And 1 Controller class:

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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.Mvc;
  4. using CompShop.Models;
  5.  
  6. namespace CompShop.Controllers
  7. {
  8. public class ItemCommentsController : Controller
  9. {
  10. // GET: ItemComments
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15.  
  16. public ItemModel GetItemDetails()
  17. {
  18. ItemModel iModel = new ItemModel()
  19. {
  20. ID = 1,
  21. Name = “HP Printer”,
  22. Category = “ComputerParts”,
  23. Price = 8840
  24. };
  25. return iModel;
  26. }
  27.  
  28. public List<BuyersCommentsModel> GetCommentList()
  29. {
  30. List<BuyersCommentsModel> CommentsList = new List<BuyersCommentsModel>();
  31. CommentsList.Add(new BuyersCommentsModel() { BuyersID = 1, BuyersName = “John”, StarRating = “4”, CommentsTitle = “Nice Product”, Comments = “I Purchased it 1 week ago and it is working fine.”, Date = Convert.ToDateTime(“27 August 17”) });
  32. CommentsList.Add(new BuyersCommentsModel() {BuyersID=2, BuyersName=“Nicki”, StarRating=“2”, CommentsTitle=“Worst Product”, Comments=“Worst Product. Don’t Buy It. I got damaged one.”, Date=Convert.ToDateTime(“12 June 17”) });
  33. CommentsList.Add(new BuyersCommentsModel() {BuyersID=3, BuyersName=“Serena”, StarRating=“3.5”, CommentsTitle=“Satisfactory”, Comments=“Go for it. It does the same job and have the less price”, Date=Convert.ToDateTime(“18 March 17”) });
  34. CommentsList.Add(new BuyersCommentsModel() {BuyersID=4, BuyersName=“William”, StarRating=“4.5”, CommentsTitle=“Superrr!!!”, Comments=“Don’t think and buy it with confidence.”, Date=Convert.ToDateTime(“11 November 16”) });
  35.  
  36. return CommentsList;
  37. }
  38.  
  39. public ActionResult ItemCommentDisplay()
  40. {
  41. }
  42. }
  43. }

1. VIEWBAG

ViewBag is a dictionary of objects and it is used for passing data from controllers to view. It stores values in a dynamic string property and you need to also use this string to retrieve values in the view page. ViewBag is used for only one-time request and once it populates the value to views it becomes null. If any redirection occurs, then also ViewBag value becomes null. There is no typecasting needed in ViewBag.

Example: Access Model Data to View
Step 1: Go to ItemCommentsController.cs and add the following highlighted action method.
  1. public ActionResult ItemCommentDisplay()
  2. {
  3. ViewBag.Item = GetItemDetails();
  4. ViewBag.Comments = GetCommentList();
  5. return View();
  6. }
Step 2: Right click on ItemCommentDisplay() Action Method and create a View.

 

 

Step 4: Now, add the following code in ItemCommentDisplay.cshtml View Page.
  1. @using CompShop.Models
  2. @{
  3. ItemModel Item = ViewBag.Item as ItemModel;
  4. List<BuyersCommentsModel> Comments = ViewBag.Comments as List<BuyersCommentsModel>;
  5. }
  6. <div>
  7. <h4><b>Pass Data using ViewBag</b></h4>
  8. <hr />
  9. <dl class=“dl-horizontal”>
  10. </dl>
  11. </div>
  12. <p>
  13. <!– ITEM DETAILS –>
  14. <div style=“background-color:burlywood; padding:5px;”>
  15. <h1 style=“color:green”>@Item.Name</h1>
  16. <b>Category:</b> @Item.Category<br />
  17. <b>Price:</b> @Item.Price
  18.  
  19. <hr />
  20. </div>
  21. <!– LOADING COMMENTS –>
  22. <h2>Comments</h2>
  23. @foreach (var comments in Comments)
  24. {
  25. <h4><b>@comments.CommentsTitle</b></h4>
  26. <b>@comments.BuyersName</b><br />
  27. <b>Date:</b> @comments.Date <span>|</span>
  28. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  29. <br />
  30. <i>@comments.Comments</i>
  31. <hr />
  32. }
  33. </p>

OUT IMAGES Given Below

 

VIEWDATA

ViewData is almost same as ViewBag and it is also used for passing models/controllers data to view. It stores data in Key-Value pair and it is also available for current request only. Means, if redirection occurs then ViewData value becomes null. You must check for null and typecast complex data type for avoiding errors.

Example: Access Model Data to View
 
Step 1: Go to ItemCommentsController.cs and add the following highlighted action method.
  1. public ActionResult ItemCommentDisplay()
  2. {
  3. ViewData[“Item”] = GetItemDetails();
  4. ViewData[“Comments”] = GetCommentList();
  5. return View();
  6. }
Step 2: Go to ItemCommentsController.cs and change the ItemCommentDisplay() action method.
 
  1. @using CompShop.Models
  2. @{
  3. ItemModel Item = ViewData[“Item”] as ItemModel;
  4. List<BuyersCommentsModel> Comments = ViewData[“Comments”] as List<BuyersCommentsModel>;
  5. }
  6. <div>
  7. <h4><b>Pass Data using ViewBag</b></h4>
  8. <hr />
  9. <dl class=“dl-horizontal”>
  10. </dl>
  11. </div>
  12. <p>
  13. <!– ITEM DETAILS –>
  14. <div style=“background-color:burlywood; padding:5px;”>
  15. @if (Item != null)
  16. {
  17. <h1 style=“color:green”>@Item.Name</h1>
  18. <b>Category:</b> @Item.Category<br />
  19. <b>Price:</b> @Item.Price
  20. }
  21. else
  22. {
  23. <h3 style=“color:red”>Sorry! Item Not Found</h3>
  24. }
  25. <hr />
  26. </div>
  27. <!– LOADING COMMENTS –>
  28. <h2>Comments</h2>
  29. @if (Comments != null)
  30. {
  31. foreach (var comments in Comments)
  32. {
  33. <h4><b>@comments.CommentsTitle</b></h4>
  34. <b>@comments.BuyersName</b><br />
  35. <b>Date:</b> @comments.Date <span>|</span>
  36. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  37. <br />
  38. <i>@comments.Comments</i>
  39. <hr />
  40. }
  41. }
  42. else
  43. {
  44. <h3 style=“color:red”>No Comments Found!</h3>
  45. }
  46. </p>

 

TEMPDATA

TempData is also used for transferring Models/Controllers data to views but it provides more functionality than ViewBag and ViewData. TempData doesn’t become null when redirection occurs and it is accessible in two continuous requests. However, TempData is mostly used for displaying Error Messages and Validation Messages. You must check for null and typecast complex data type in order to avoid errors.

Example: Access Model Data to View
Step 1: Go to ItemCommentsController.cs and add the following highlighted action method.
  1. public ActionResult ItemCommentDisplay()
  2. {
  3. TempData[“Item”] = GetItemDetails();
  4. TempData[“Comments”] = GetCommentList();
  5. return View();
  6. }
Step 2: Go to ItemCommentsController.cs and change the ItemCommentDisplay() action method.
  1. @using CompShop.Models
  2. @{
  3. ItemModel Item = TempData[“Item”] as ItemModel;
  4. List<BuyersCommentsModel> Comments = TempData[“Comments”] as List<BuyersCommentsModel>;
  5. }
  6. <div>
  7. <h4><b>Pass Data using ViewBag</b></h4>
  8. <hr />
  9. <dl class=“dl-horizontal”>
  10. </dl>
  11. </div>
  12. <p>
  13. <!– ITEM DETAILS –>
  14. <div style=“background-color:burlywood; padding:5px;”>
  15. @if (Item != null)
  16. {
  17. <h1 style=“color:green”>@Item.Name</h1>
  18. <b>Category:</b> @Item.Category<br />
  19. <b>Price:</b> @Item.Price
  20. }
  21. else
  22. {
  23. <h3 style=“color:red”>Sorry! Item Not Found</h3>
  24. }
  25. <hr />
  26. </div>
  27. <!– LOADING COMMENTS –>
  28. <h2>Comments</h2>
  29. @if (Comments != null)
  30. {
  31. foreach (var comments in Comments)
  32. {
  33. <h4><b>@comments.CommentsTitle</b></h4>
  34. <b>@comments.BuyersName</b><br />
  35. <b>Date:</b> @comments.Date <span>|</span>
  36. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  37. <br />
  38. <i>@comments.Comments</i>
  39. <hr />
  40. }
  41. }
  42. else
  43. {
  44. <h3 style=“color:red”>No Comments Found!</h3>
  45. }
  46. </p>

SUMMARY

In this chapter, you learned how to pass multiple models data to view using ViewBag, ViewData and TempData. However, there are also various ways to achieve this goal and in the next chapter, you will learn How to use Session to Pass Model to Views.

Pass Data Using Session, Tuples, And Dynamic(ExpandoObject) In ASP.NET MVC
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.
 
  1. public ActionResult ItemCommentDisplay()
  2. {
  3. Session[“Item”] = GetItemDetails();
  4. Session[“Comments”] = GetCommentList();
  5. return View();
  6. }
Step 2: Right click on ItemCommentDisplay() Action Method and create a View.
 
 
Step 4: Now, add the following code in ItemCommentDisplay.cshtml View Page.
  1. @using CompShop.Models
  2. @{
  3. ItemModel Item = Session[“Item”] as ItemModel;
  4. List<BuyersCommentsModel> Comments = Session[“Comments”] as List<BuyersCommentsModel>;
  5. }
  6. <!– ITEM DETAILS –>
  7. <div style=“background-color:burlywood; padding:5px;”>
  8. @if (Item != null)
  9. {
  10. <h1 style=“color:green”>@Item.Name</h1>
  11. <b>Category:</b> @Item.Category<br />
  12. <b>Price:</b> @Item.Price
  13. }
  14. else
  15. {
  16. <h3 style=“color:red”>Sorry! Item Not Found</h3>
  17. }
  18. <hr />
  19. </div>
  20. <!– LOADING COMMENTS –>
  21. <h2>Comments</h2>
  22. @if (Comments != null)
  23. {
  24. foreach (var comments in Comments)
  25. {
  26. <h4><b>@comments.CommentsTitle</b></h4>
  27. <b>@comments.BuyersName</b><br />
  28. <b>Date:</b> @comments.Date <span>|</span>
  29. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  30. <br />
  31. <i>@comments.Comments</i>
  32. <hr />
  33. }
  34. }
  35. else
  36. {
  37. <h3 style=“color:red”>No Comments Found!</h3>
  38. }
 
 
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.

Step 1: Go to ItemCommentsController.cs and change the ItemCommentDisplay() action method.
 
  1. public ActionResult ItemCommentDisplay()
  2. {
  3. var commentdisplay = new Tuple<ItemModel, List<BuyersCommentsModel>>(GetItemDetails(), GetCommentList());
  4. return View(commentdisplay);
  5. }

Let’s understand this code

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.

  1. var commentdisplay = new Tuple<ItemModel, List<BuyersCommentsModel>>(GetItemDetails(),

Step 2: Now, add the following code in ItemCommentDisplay.cshtml View Page.

 

 

  1. @using CompShop.Models
  2. @model Tuple<ItemModel, List<BuyersCommentsModel>>
  3.  
  4. <div>
  5. <h4><b>Pass Data using Tuples</b></h4>
  6. <hr />
  7. <dl class=“dl-horizontal”>
  8. </dl>
  9. </div>
  10. <p>
  11. <!– ITEM DETAILS –>
  12. <div style=“background-color:burlywood; padding:5px;”>
  13.  
  14. <h1 style=“color:green”>@Model.Item1.Name</h1>
  15. <b>Category:</b> @Model.Item1.Category<br />
  16. <b>Price:</b> @Model.Item1.Price
  17.  
  18. <hr />
  19. </div>
  20. <!– LOADING COMMENTS –>
  21. <h2>Comments</h2>
  22.  
  23. @foreach (var comments in Model.Item2)
  24. {
  25. <h4><b>@comments.CommentsTitle</b></h4>
  26. <b>@comments.BuyersName</b><br />
  27. <b>Date:</b> @comments.Date <span>|</span>
  28. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  29. <br />
  30. <i>@comments.Comments</i>
  31. <hr />
  32. }
  33.  
  34. </p>


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

  1. @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>

 

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.

 

Step 1: Go to ItemCommentsController.cs and change the ItemCommentDisplay() action method.

 

  1. public ActionResult ItemCommentDisplay()
  2. {
  3. dynamic cmnt = new ExpandoObject();
  4. cmnt.Item = GetItemDetails();
  5. cmnt.Comments = GetCommentList();
  6. return View(cmnt);
  7. }

 

Step 2: Now, add the following code in ItemCommentDisplay.cshtml View Page.

 

  1. @using CompShop.Models
  2. @model dynamic
  3.  
  4. <div>
  5. <h4><b>Pass Data using Dynamic</b></h4>
  6. <hr />
  7. <dl class=“dl-horizontal”>
  8. </dl>
  9. </div>
  10. <p>
  11. <!– ITEM DETAILS –>
  12. <div style=“background-color:burlywood; padding:5px;”>
  13.  
  14. <h1 style=“color:green”>@Model.Item.Name</h1>
  15. <b>Category:</b> @Model.Item.Category<br />
  16. <b>Price:</b> @Model.Item.Price
  17.  
  18. <hr />
  19. </div>
  20. <!– LOADING COMMENTS –>
  21. <h2>Comments</h2>
  22.  
  23. @foreach (var comments in Model.Comments)
  24. {
  25. <h4><b>@comments.CommentsTitle</b></h4>
  26. <b>@comments.BuyersName</b><br />
  27. <b>Date:</b> @comments.Date <span>|</span>
  28. <b>Star Rating: </b>@comments.StarRating<span>/5</span>
  29. <br />
  30. <i>@comments.Comments</i>
  31. <hr />
  32. }
  33.  
  34. </p>

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.