Techniques to pass multiple
models to view
Passing multiple models from controller to View can be done by the following ways
- Using ViewModel.
- Using ViewData.
- Using ViewBag.
- Using Tuple.
- Using Dynamic Model.
Solution Screen Shot

MultiplemodelDemoContriller
- public class MultipleModelsDemoController: Controller
- {
- // GET: /MultipleModelsDemo/
- public ActionResult UsingViewModel()
- {
- ViewModel vmodel = new ViewModel();
- vmodel.Employees = Employee.GetEmployee();
- vmodel.Departments = Department.GetDepartment();
- return View(vmodel);
- }
- public ActionResult UsingViewData()
- {
- ViewData["Employees"] = Employee.GetEmployee();
- ViewData["Departments"] = Department.GetDepartment();
- return View();
- }
- public ActionResult UsingViewBag()
- {
- ViewBag.Employees = Employee.GetEmployee();
- ViewBag.Department = Department.GetDepartment();
- return View();
- }
- public ActionResult UsingTuple()
- {
- var model = new Tuple < List < Employee > ,
- List < Department >> (Employee.GetEmployee(), Department.GetDepartment());
- return View(model);
- }
- public ActionResult UsingDynamicModel()
- {
- //ExpandoObject class, in the System.Dynamic namespace. What makes this special is,
- //it allows you to create an object with members that can be dynamically added and removed at run time.
- dynamic mymodel = new ExpandoObject();
- mymodel.Employee = Employee.GetEmployee();
- mymodel.Department = Department.GetDepartment();
- return View(mymodel);
- }
Department.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Mvc_Security.Models
- {
- public class Department
- {
- public int DeptId
- {
- get;
- set;
- }
- public string DeptName
- {
- get;
- set;
- }
- public static List < Department > GetDepartment()
- {
- List < Department > dept = new List < Department > ();
- dept.Add(new Department
- {
- DeptId = 1, DeptName = "HR"
- });
- dept.Add(new Department {
- DeptId = 2, DeptName = "Finance"
- });
- return dept;
- }
- }
- }
Employee
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Mvc_Security.Models
- {
- public class Employee
- {
- public int EmpId
- {get;set;}
- public string EmpName
- {get;set;}
- public static List < Employee > GetEmployee()
- {
- List < Employee > emp = new List < Employee > ();
- emp.Add(new Employee
- {
- EmpId = 101, EmpName = "Deb"
- });
- emp.Add(new Employee {
- EmpId = 102, EmpName = "Ashu"
- });
- return emp;
- }
- }
- }
ViewModels.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Mvc_Security.Models
- {
- public class ViewModel
- {
- public IEnumerable < Employee > Employees {
- get;
- set;
- }
- public IEnumerable < Department > Departments {
- get;
- set;
- }
- }
- }
Using Dynamic Model.cshtml
- @using Mvc_Security.Models;
- @model dynamic
- @{
- ViewBag.Title = "UsingDynamicModel";
- }
- <h2>UsingDynamicModel</h2>
- <p>
- <b>Employee List</b>
- </p>
- <table>
- <tr>
- <th>EmpId</th>
- <th>EmpName</th>
- </tr>
- @foreach (Employee emp in Model.Employee)
- {
- <tr>
- <td>@emp.EmpId</td>
- <td>@emp.EmpName</td>
- </tr>
- }
- </table>
- <p>
- <b>Department List</b>
- </p>
- <table>
- <tr>
- <th>DeptId</th>
- <th>DeptName</th>
- </tr>
- @foreach (Department dept in Model.Department)
- {
- <tr>
- <td>@dept.DeptId</td>
- <td>@dept.DeptName</td>
- </tr>
- }
- </table>
UsingTuple.cshtml
- @using Mvc_Security.Models
- @model Tuple
- <List
- <Employee>, List
- <Department>>
- @{
- ViewBag.Title = "UsingTuple";
- }
- <h2>UsingTuple</h2>
- <p>
- <b>Employee List</b>
- </p>
- <table>
- <tr>
- <th>EmpId</th>
- <th>EmpName</th>
- </tr>
- @foreach (Employee emp in Model.Item1)
- {
- <tr>
- <td>@emp.EmpId</td>
- <td>@emp.EmpName</td>
- </tr>
- }
- </table>
- <p>
- <b>Department List</b>
- </p>
- <table>
- <tr>
- <th>DeptId</th>
- <th>DeptName</th>
- </tr>
- @foreach (Department dept in Model.Item2)
- {
- <tr>
- <td>@dept.DeptId</td>
- <td>@dept.DeptName</td>
- </tr>
- }
- </table>
UsingViewBag.cshtml
- @using Mvc_Security.Models
- @{
- ViewBag.Title = "UsingViewBag";
- }
- <h2>UsingViewBag</h2>
- <p>
- <b>Employee List</b>
- </p>
- <table>
- <tr>
- <th>EmpId</th>
- <th>EmpName</th>
- </tr>
- @foreach (Employee emp in ViewBag.Employees)
- {
- <tr>
- <td>@emp.EmpId</td>
- <td>@emp.EmpName</td>
- </tr>
- }
- </table>
- <p>
- <b>Department List</b>
- </p>
- <table>
- <tr>
- <th>DeptId</th>
- <th>DeptName</th>
- </tr>
- @foreach (Department dept in ViewBag.Departments)
- {
- <tr>
- <td>@dept.DeptId</td>
- <td>@dept.DeptName</td>
- </tr>
- }
- </table>
UsingViewData.cshtml
- @using Mvc_Security.Models;
- @{
- ViewBag.Title = "UsingViewData";
- }
- <h2>UsingViewData</h2>
- @{
- IEnumerable
- <Employee> employees = ViewData["Employees"] as IEnumerable
- <Employee>;
- IEnumerable
- <Department> departments = ViewData["Departments"] as IEnumerable
- <Department>;
- }
- <p>
- <b>Employee List</b>
- </p>
- <table>
- <tr>
- <th>EmpId</th>
- <th>EmpName</th>
- </tr>
- @foreach (Employee emp in employees)
- {
- <tr>
- <td>@emp.EmpId</td>
- <td>@emp.EmpName</td>
- </tr>
- }
- </table>
- <p>
- <b>Department List</b>
- </p>
- <table>
- <tr>
- <th>DeptId</th>
- <th>DeptName</th>
- </tr>
- @foreach (Department dept in departments)
- {
- <tr>
- <td>@dept.DeptId</td>
- <td>@dept.DeptName</td>
- </tr>
- }
- </table>
UsingViewmodel.cshtml
- @using Mvc_Security.Models;
- @model ViewModel
- @{
- ViewBag.Title = "UsingViewModel";
- }
- <h2>UsingViewModel</h2>
- <p>
- <b>Employee List</b>
- </p>
- <table>
- <tr>
- <th>EmpId</th>
- <th>EmpName</th>
- </tr>
- @foreach (Employee emp in Model.Employees)
- {
- <tr>
- <td>@emp.EmpId</td>
- <td>@emp.EmpName</td>
- </tr>
- }
- </table>
- <p>
- <b>Department List</b>
- </p>
- <table>
- <tr>
- <th>DeptId</th>
- <th>DeptName</th>
- </tr>
- @foreach (Department dept in Model.Departments)
- {
- <tr>
- <td>@dept.DeptId</td>
- <td>@dept.DeptName</td>
- </tr>
- }
- </table>

Debasish TutuPosted Apr 28, 2015, 2:43 AM
thanks Manoj and SanthaKumar
Manoj BhoirPosted Apr 25, 2015, 12:46 AM
Good One
Santhakumar MunuswamyPosted Apr 25, 2015, 12:27 AM
I think it is should be under article category
Santhakumar MunuswamyPosted Apr 25, 2015, 12:27 AM
Good work