Passing Multiple Models to Views

Techniques to pass multiple models to view

Passing multiple models from controller to View can be done by the following ways
  1. Using ViewModel.
  2. Using ViewData.
  3. Using ViewBag.
  4. Using Tuple.
  5. Using Dynamic Model.
 Solution Screen Shot
 


MultiplemodelDemoContriller 
  1. public class MultipleModelsDemoController: Controller  
  2. {  
  3.     // GET: /MultipleModelsDemo/  
  4.     public ActionResult UsingViewModel()   
  5.     {  
  6.         ViewModel vmodel = new ViewModel();  
  7.         vmodel.Employees = Employee.GetEmployee();  
  8.         vmodel.Departments = Department.GetDepartment();  
  9.         return View(vmodel);  
  10.     }  
  11.     public ActionResult UsingViewData()   
  12.     {  
  13.         ViewData["Employees"] = Employee.GetEmployee();  
  14.         ViewData["Departments"] = Department.GetDepartment();  
  15.         return View();  
  16.     }  
  17.     public ActionResult UsingViewBag()  
  18.     {  
  19.         ViewBag.Employees = Employee.GetEmployee();  
  20.         ViewBag.Department = Department.GetDepartment();  
  21.         return View();  
  22.     }  
  23.     public ActionResult UsingTuple()  
  24.     {  
  25.         var model = new Tuple < List < Employee > ,  
  26.             List < Department >> (Employee.GetEmployee(), Department.GetDepartment());  
  27.         return View(model);  
  28.     }  
  29.     public ActionResult UsingDynamicModel()  
  30.     {  
  31.         //ExpandoObject class, in the System.Dynamic namespace. What makes this special is,  
  32.         //it allows you to create an object with members that can be dynamically added and removed at run time.  
  33.         dynamic mymodel = new ExpandoObject();  
  34.         mymodel.Employee = Employee.GetEmployee();  
  35.         mymodel.Department = Department.GetDepartment();  
  36.         return View(mymodel);  
  37.     }  
Department.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace Mvc_Security.Models  
  6. {  
  7.     public class Department  
  8.     {  
  9.         public int DeptId   
  10.         {  
  11.             get;  
  12.             set;  
  13.         }  
  14.         public string DeptName  
  15.         {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public static List < Department > GetDepartment()  
  20.         {  
  21.             List < Department > dept = new List < Department > ();  
  22.             dept.Add(new Department   
  23.             {  
  24.                 DeptId = 1, DeptName = "HR"  
  25.             });  
  26.             dept.Add(new Department {  
  27.                 DeptId = 2, DeptName = "Finance"  
  28.             });  
  29.             return dept;  
  30.         }  
  31.     }  
  32. }  

Employee 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace Mvc_Security.Models  
  6. {  
  7.     public class Employee  
  8.     {  
  9.         public int EmpId   
  10.         {get;set;}  
  11.         public string EmpName   
  12.         {get;set;}  
  13.         public static List < Employee > GetEmployee()  
  14.         {  
  15.             List < Employee > emp = new List < Employee > ();  
  16.             emp.Add(new Employee   
  17.             {  
  18.                 EmpId = 101, EmpName = "Deb"  
  19.             });  
  20.             emp.Add(new Employee {  
  21.                 EmpId = 102, EmpName = "Ashu"  
  22.             });  
  23.             return emp;  
  24.         }  
  25.     }  
  26. }  

ViewModels.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace Mvc_Security.Models  
  6. {  
  7.     public class ViewModel  
  8.     {  
  9.         public IEnumerable < Employee > Employees {  
  10.             get;  
  11.             set;  
  12.         }  
  13.         public IEnumerable < Department > Departments {  
  14.             get;  
  15.             set;  
  16.         }  
  17.     }  
  18. }  

 Using Dynamic Model.cshtml 

  1. @using Mvc_Security.Models;  
  2. @model dynamic  
  3. @{  
  4. ViewBag.Title = "UsingDynamicModel";  
  5. }  
  6. <h2>UsingDynamicModel</h2>  
  7. <p>  
  8.     <b>Employee List</b>  
  9. </p>  
  10. <table>  
  11.     <tr>  
  12.         <th>EmpId</th>  
  13.         <th>EmpName</th>  
  14.     </tr>  
  15. @foreach (Employee emp in Model.Employee)  
  16. {  
  17.     <tr>  
  18.         <td>@emp.EmpId</td>  
  19.         <td>@emp.EmpName</td>  
  20.     </tr>  
  21. }  
  22. </table>  
  23. <p>  
  24.     <b>Department List</b>  
  25. </p>  
  26. <table>  
  27.     <tr>  
  28.         <th>DeptId</th>  
  29.         <th>DeptName</th>  
  30.     </tr>  
  31. @foreach (Department dept in Model.Department)  
  32. {  
  33.     <tr>  
  34.         <td>@dept.DeptId</td>  
  35.         <td>@dept.DeptName</td>  
  36.     </tr>  
  37. }  
  38. </table>  

 UsingTuple.cshtml

  1. @using Mvc_Security.Models  
  2. @model Tuple  
  3. <List  
  4.     <Employee>, List  
  5.         <Department>>  
  6. @{  
  7. ViewBag.Title = "UsingTuple";  
  8. }  
  9.             <h2>UsingTuple</h2>  
  10.             <p>  
  11.                 <b>Employee List</b>  
  12.             </p>  
  13.             <table>  
  14.                 <tr>  
  15.                     <th>EmpId</th>  
  16.                     <th>EmpName</th>  
  17.                 </tr>  
  18. @foreach (Employee emp in Model.Item1)  
  19. {  
  20.                 <tr>  
  21.                     <td>@emp.EmpId</td>  
  22.                     <td>@emp.EmpName</td>  
  23.                 </tr>  
  24. }  
  25.             </table>  
  26.             <p>  
  27.                 <b>Department List</b>  
  28.             </p>  
  29.             <table>  
  30.                 <tr>  
  31.                     <th>DeptId</th>  
  32.                     <th>DeptName</th>  
  33.                 </tr>  
  34. @foreach (Department dept in Model.Item2)  
  35. {  
  36.                 <tr>  
  37.                     <td>@dept.DeptId</td>  
  38.                     <td>@dept.DeptName</td>  
  39.                 </tr>  
  40. }  
  41.             </table>  

UsingViewBag.cshtml 

  1. @using Mvc_Security.Models  
  2. @{  
  3. ViewBag.Title = "UsingViewBag";  
  4. }  
  5. <h2>UsingViewBag</h2>  
  6. <p>  
  7.     <b>Employee List</b>  
  8. </p>  
  9. <table>  
  10.     <tr>  
  11.         <th>EmpId</th>  
  12.         <th>EmpName</th>  
  13.     </tr>  
  14. @foreach (Employee emp in ViewBag.Employees)  
  15. {  
  16.     <tr>  
  17.         <td>@emp.EmpId</td>  
  18.         <td>@emp.EmpName</td>  
  19.     </tr>  
  20. }  
  21. </table>  
  22. <p>  
  23.     <b>Department List</b>  
  24. </p>  
  25. <table>  
  26.     <tr>  
  27.         <th>DeptId</th>  
  28.         <th>DeptName</th>  
  29.     </tr>  
  30. @foreach (Department dept in ViewBag.Departments)  
  31. {  
  32.     <tr>  
  33.         <td>@dept.DeptId</td>  
  34.         <td>@dept.DeptName</td>  
  35.     </tr>  
  36. }  
  37. </table>

UsingViewData.cshtml 

  1. @using Mvc_Security.Models;  
  2. @{  
  3. ViewBag.Title = "UsingViewData";  
  4. }  
  5. <h2>UsingViewData</h2>  
  6. @{  
  7. IEnumerable  
  8. <Employee> employees = ViewData["Employees"] as IEnumerable  
  9.     <Employee>;  
  10. IEnumerable  
  11.         <Department> departments = ViewData["Departments"] as IEnumerable  
  12.             <Department>;  
  13. }  
  14.                 <p>  
  15.                     <b>Employee List</b>  
  16.                 </p>  
  17.                 <table>  
  18.                     <tr>  
  19.                         <th>EmpId</th>  
  20.                         <th>EmpName</th>  
  21.                     </tr>  
  22. @foreach (Employee emp in employees)  
  23. {  
  24.                     <tr>  
  25.                         <td>@emp.EmpId</td>  
  26.                         <td>@emp.EmpName</td>  
  27.                     </tr>  
  28. }  
  29.                 </table>  
  30.                 <p>  
  31.                     <b>Department List</b>  
  32.                 </p>  
  33.                 <table>  
  34.                     <tr>  
  35.                         <th>DeptId</th>  
  36.                         <th>DeptName</th>  
  37.                     </tr>  
  38. @foreach (Department dept in departments)  
  39. {  
  40.                     <tr>  
  41.                         <td>@dept.DeptId</td>  
  42.                         <td>@dept.DeptName</td>  
  43.                     </tr>  
  44. }  
  45.                 </table>  

 UsingViewmodel.cshtml 

  1. @using Mvc_Security.Models;  
  2. @model ViewModel  
  3. @{  
  4. ViewBag.Title = "UsingViewModel";  
  5. }  
  6. <h2>UsingViewModel</h2>  
  7. <p>  
  8.     <b>Employee List</b>  
  9. </p>  
  10. <table>  
  11.     <tr>  
  12.         <th>EmpId</th>  
  13.         <th>EmpName</th>  
  14.     </tr>  
  15. @foreach (Employee emp in Model.Employees)  
  16. {  
  17.     <tr>  
  18.         <td>@emp.EmpId</td>  
  19.         <td>@emp.EmpName</td>  
  20.     </tr>  
  21. }  
  22. </table>  
  23. <p>  
  24.     <b>Department List</b>  
  25. </p>  
  26. <table>  
  27.     <tr>  
  28.         <th>DeptId</th>  
  29.         <th>DeptName</th>  
  30.     </tr>  
  31. @foreach (Department dept in Model.Departments)  
  32. {  
  33.     <tr>  
  34.         <td>@dept.DeptId</td>  
  35.         <td>@dept.DeptName</td>  
  36.     </tr>  
  37. }  
  38. </table>


Ebook Download
View all
Learn
View all