Namespaces
You will need to import the following namespaces.
using System.Dynamic;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
Model
Following are the two Model classes.
CustomerModel
The following Model class will be used to populate data from Customers Table.
public class CustomerModel
{
public string CustomerId { get; set; }
public string CustomerName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
EmployeeModel
The following Model class will be used to populate data from Employees Table.
public class EmployeeModel
{
public string EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Controller
The Controller consists of an Index Action method. Inside this Action method, first an object of ExpandoObject class is created and it's instance is assigned to a variable of type Dynamic.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
dynamic model = new ExpandoObject();
model.Customers = GetCustomers();
model.Employees = GetEmployees();
return View(model);
}
private static List<CustomerModel> GetCustomers()
{
List<CustomerModel> customers = new List<CustomerModel>();
string query = "SELECT TOP 10 CustomerID, ContactName, City, Country FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new CustomerModel
{
CustomerId = sdr["CustomerID"].ToString(),
CustomerName = sdr["ContactName"].ToString(),
City = sdr["City"].ToString(),
Country = sdr["Country"].ToString()
});
}
}
con.Close();
return customers;
}
}
}
private static List<EmployeeModel> GetEmployees()
{
List<EmployeeModel> employees = new List<EmployeeModel>();
string query = "SELECT EmployeeID, (FirstName + ' ' + LastName) [Name], City, Country FROM Employees";
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
employees.Add(new EmployeeModel
{
EmployeeId = sdr["EmployeeID"].ToString(),
EmployeeName = sdr["Name"].ToString(),
City = sdr["City"].ToString(),
Country = sdr["Country"].ToString()
});
}
con.Close();
return employees;
}
}
}
}
}
View
Inside the View, first you will need to import the namespace for accessing the Model classes.
Then you will need to declare the Model for the View as dynamic.
For displaying the records, two HTML Tables are used and by iterating over the Generic List Collection of Model objects, rows are added to the HTML Tables.
@using Multiple_Model_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 (CustomerModel customer in Model.Customers)
{
<tr>
<td>@customer.CustomerId</td>
<td>@customer.CustomerName</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 (EmployeeModel employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.EmployeeName</td>
<td>@employee.City</td>
<td>@employee.Country</td>
</tr>
}
</table>
</body>
</html>
Screenshot