Hi rani,
You can do it by 2 ways.
1. You can use dynamic ExpandoObject class object to sent multiple model to the View.
2. Create another model class to pass to the view.
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Using ExpandoObject
Model
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
List<Customer> customers = this.Context.Customers.Take(10).ToList();
List<Employee> employees = this.Context.Employees.Take(10).ToList();
dynamic model = new System.Dynamic.ExpandoObject();
model.Customers = customers;
model.Employees = employees;
return View(model);
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using Multiple_Model_Core_MVC.Models
@model dynamic
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<th>CustomerID</th>
<th>Contact Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model.Customers)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
</tr>
}
</table>
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>EmployeeID</th>
<th>Employee Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Employee employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeID</td>
<td>@employee.FirstName @employee.LastName</td>
<td>@employee.City</td>
<td>@employee.Country</td>
</tr>
}
</table>
</body>
</html>
Using Model class
Model
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
public class DetailModel
{
public List<Customer> Customers { get; set; }
public List<Employee> Employees { get; set; }
}
Controller
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
List<Customer> customers = this.Context.Customers.Take(10).ToList();
List<Employee> employees = this.Context.Employees.Take(10).ToList();
DetailModel model = new DetailModel();
model.Customers = customers;
model.Employees = employees;
return View(model);
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using Multiple_Model_Core_MVC.Models
@model DetailModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<th>CustomerID</th>
<th>Contact Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model.Customers)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
</tr>
}
</table>
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>EmployeeID</th>
<th>Employee Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Employee employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeID</td>
<td>@employee.FirstName @employee.LastName</td>
<td>@employee.City</td>
<td>@employee.Country</td>
</tr>
}
</table>
</body>
</html>
Screenshot