Multiple Model In A Single View In MVC
17:19Introduction
In this post I want to show how to use multiple model in a single view.View Image
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC_Multiple_Model.Models
{
public class Client
{
public int ClientId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public static List GetClients()
{
List clients = new List();
clients.Add(new Client { ClientId = 1, Code = "C001", Name = "Amit Bhunia" });
clients.Add(new Client { ClientId = 2, Code = "C002", Name = "Abhijit Bera" });
clients.Add(new Client { ClientId = 3, Code = "C003", Name = "Asis Bera" });
return clients;
}
}
public class Employee
{
public int EmployeeId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string EnrollmentNo { get; set; }
public static List GetEmployees()
{
List employees = new List();
employees.Add(new Employee { EmployeeId = 1, Code = "E0001", Name = "Surajit Ghosh", EnrollmentNo = "201404150001" });
employees.Add(new Employee { EmployeeId = 2, Code = "E0002", Name = "Sourav Mondal", EnrollmentNo = "201404150002" });
employees.Add(new Employee { EmployeeId = 3, Code = "E0003", Name = "Sankar Parida", EnrollmentNo = "201404150003" });
return employees;
}
}
public class ViewModel
{
public IEnumerable Clients { get; set; }
public IEnumerable Employees { get; set; }
}
}
1. Using View Model
View:@using MVC_Multiple_Model.Models
@{
ViewBag.Title = "Home Page";
}
<style>
table tbody th {
background-color: #808080;
color: #fff;
padding: 5px;
}
table tbody tr td {
border-bottom: 1px solid blue;
padding: 5px;
background-color: #b9dbee;
}
div {
margin-left: 36%;
margin-top: 5%;
}
</style>
<div>
<h2>@ViewBag.Message</h2>
<p><b>Client List</b></p>
<table>
<tr>
<th>Client_Id</th>
<th>Client_Code</th>
<th>Client_Name</th>
</tr>
@foreach (Client client in Model.Clients)
{
<tr>
<td>@client.ClientId</td>
<td>@client.Code</td>
<td>@client.Name</td>
</tr>
}
</table>
<p><b>Employee List</b></p>
<table>
<tr>
<th>Emp_Id</th>
<th>Emp_Code</th>
<th>Emp_Name</th>
<th>Emp_Enrollment_No</th>
</tr>
@foreach (Employee employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Code</td>
<td>@employee.Name</td>
<td>@employee.EnrollmentNo</td>
</tr>
}
</table>
</div>
Controller:using MVC_Multiple_Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_Multiple_Model.Controllers
{
public class TestController : Controller
{
public ActionResult Method1()
{
ViewBag.Message = "Using View Model";
ViewModel mymodel = new ViewModel();
mymodel.Clients = Client.GetClients();
mymodel.Employees = Employee.GetEmployees();
return View(mymodel);
}
}
}
2. Using View Data
View:@using MVC_Multiple_Model.Models;
@{
ViewBag.Title = "Home Page";
}
<style>
table tbody th {
background-color: #808080;
color: #fff;
padding: 5px;
}
table tbody tr td {
border-bottom: 1px solid blue;
padding: 5px;
background-color: #b9dbee;
}
div {
margin-left: 36%;
margin-top: 5%;
}
</style>
<div>
<h2>@ViewBag.Message</h2>
<p><b>Client List</b></p>
@{
IEnumerable<Client> clients = ViewData["Clients"] as IEnumerable<Client>;
IEnumerable<Employee> employees = ViewData["Employees"] as IEnumerable<Employee>;
}
<table>
<tr>
<th>Client_Id</th>
<th>Client_Code</th>
<th>Client_Name</th>
</tr>
@foreach (Client client in clients)
{
<tr>
<td>@client.ClientId</td>
<td>@client.Code</td>
<td>@client.Name</td>
</tr>
}
</table>
<p><b>Employee List</b></p>
<table>
<tr>
<th>Emp_Id</th>
<th>Emp_Code</th>
<th>Emp_Name</th>
<th>Emp_Enrollment_No</th>
</tr>
@foreach (Employee employee in employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Code</td>
<td>@employee.Name</td>
<td>@employee.EnrollmentNo</td>
</tr>
}
</table>
</div>
Controller:using MVC_Multiple_Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_Multiple_Model.Controllers
{
public class TestController : Controller
{
public ActionResult Method2()
{
ViewBag.Message = "Using View Data";
ViewData["Clients"] = Client.GetClients();
ViewData["Employees"] = Employee.GetEmployees();
return View();
}
}
}
3. Using View Bag
View:@using MVC_Multiple_Model.Models
@{
ViewBag.Title = "Home Page";
}
<style>
table tbody th {
background-color: #808080;
color: #fff;
padding: 5px;
}
table tbody tr td {
border-bottom: 1px solid blue;
padding: 5px;
background-color: #b9dbee;
}
div {
margin-left: 36%;
margin-top: 5%;
}
</style>
<div>
<h2>@ViewBag.Message</h2>
<p><b>Client List</b></p>
<table>
<tr>
<th>Client_Id</th>
<th>Client_Code</th>
<th>Client_Name</th>
</tr>
@foreach (Client client in ViewBag.Clients)
{
<tr>
<td>@client.ClientId</td>
<td>@client.Code</td>
<td>@client.Name</td>
</tr>
}
</table>
<p><b>Employee List</b></p>
<table>
<tr>
<th>Emp_Id</th>
<th>Emp_Code</th>
<th>Emp_Name</th>
<th>Emp_Enrollment_No</th>
</tr>
@foreach (Employee employee in ViewBag.Employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Code</td>
<td>@employee.Name</td>
<td>@employee.EnrollmentNo</td>
</tr>
}
</table>
</div>
Controller:using MVC_Multiple_Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_Multiple_Model.Controllers
{
public class TestController : Controller
{
public ActionResult Method3()
{
ViewBag.Message = "Using View Bag";
ViewBag.Clients = Client.GetClients();
ViewBag.Employees = Employee.GetEmployees();
return View();
}
}
4. Using Tuple
View:@using MVC_Multiple_Model.Models;
@model Tuple<List<Client>, List<Employee>>
@{
ViewBag.Title = "Home Page";
}
<style>
table tbody th {
background-color: #808080;
color: #fff;
padding: 5px;
}
table tbody tr td {
border-bottom: 1px solid blue;
padding: 5px;
background-color: #b9dbee;
}
div {
margin-left: 36%;
margin-top: 5%;
}
</style>
<div>
<h2>@ViewBag.Message</h2>
<p><b>Client List</b></p>
<table>
<tr>
<th>Client_Id</th>
<th>Client_Code</th>
<th>Client_Name</th>
</tr>
@foreach (Client client in Model.Item1)
{
<tr>
<td>@client.ClientId</td>
<td>@client.Code</td>
<td>@client.Name</td>
</tr>
}
</table>
<p><b>Employee List</b></p>
<table>
<tr>
<th>Emp_Id</th>
<th>Emp_Code</th>
<th>Emp_Name</th>
<th>Emp_Enrollment_No</th>
</tr>
@foreach (Employee employee in Model.Item2)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Code</td>
<td>@employee.Name</td>
<td>@employee.EnrollmentNo</td>
</tr>
}
</table>
</div>
Controller:using MVC_Multiple_Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_Multiple_Model.Controllers
{
public class TestController : Controller
{
public ActionResult Method4()
{
ViewBag.Message = "Using Tuple";
var tupleModel = new Tuple, List>(Client.GetClients(), Employee.GetEmployees());
return View(tupleModel);
}
}
}
5. Using Dynamic
View:@using MVC_Multiple_Model.Models
@model dynamic
@{
ViewBag.Title = "Home Page";
}
<style>
table tbody th {
background-color: #808080;
color: #fff;
padding: 5px;
}
table tbody tr td {
border-bottom: 1px solid blue;
padding: 5px;
background-color: #b9dbee;
}
div {
margin-left: 36%;
margin-top: 5%;
}
</style>
<div>
<h2>@ViewBag.Message</h2>
<p><b>Client List</b></p>
<table>
<tr>
<th>Client_Id</th>
<th>Client_Code</th>
<th>Client_Name</th>
</tr>
@foreach (Client client in Model.Clients)
{
<tr>
<td>@client.ClientId</td>
<td>@client.Code</td>
<td>@client.Name</td>
</tr>
}
</table>
<p><b>Employee List</b></p>
<table>
<tr>
<th>Emp_Id</th>
<th>Emp_Code</th>
<th>Emp_Name</th>
<th>Emp_Enrollment_No</th>
</tr>
@foreach (Employee employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Code</td>
<td>@employee.Name</td>
<td>@employee.EnrollmentNo</td>
</tr>
}
</table>
</div>
Controller:using MVC_Multiple_Model.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_Multiple_Model.Controllers
{
public class TestController : Controller
{
public ActionResult Method5()
{
ViewBag.Message = "Using Dynamic";
dynamic mymodel = new ExpandoObject();
mymodel.Clients = Client.GetClients();
mymodel.Employees = Employee.GetEmployees();
return View(mymodel);
}
}
}


0 comments