Model
Following are the
two Model classes.
Customer
The following Model
class will be used to populate data from Customers Table.
public class Customer
{
public string CustomerId { get; set; }
public string CustomerName
{ get; set; }
public string City { get; set; }
}
Employee
The following Model
class will be used to populate data from Employees Table.
public class Employee
{
public string EmployeeId { get; set; }
public string EmployeeName
{ get; set; }
public string City { 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 Dynamically
Note: ExpandoObject allows
to add and remove objects dynamically at runtime. It is member of System.Dynamic namespace.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
dynamic model = new System.Dynamic.ExpandoObject();
model.Customers = GetCustomers();
model.Employees = GetEmployees();
return View(model);
}
private static List<Customer> GetCustomers()
{
List<Customer> customers = new List<Customer>();
string query = "SELECT CustomerID, ContactName, City FROM
Customers";
string constr = ConfigurationManager.ConnectionStrings["Con1"].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 Customer
{
CustomerId = sdr["CustomerID"].ToString(),
CustomerName = sdr["ContactName"].ToString(),
City = sdr["City"].ToString()
});
}
}
con.Close();
return customers;
}
}
}
private static List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
string query = "SELECT EmployeeID, EmployeeName [Name],
City FROM Employees";
string constr = ConfigurationManager.ConnectionStrings["Con1"].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 Employee
{
EmployeeId = sdr["EmployeeID"].ToString(),
EmployeeName = sdr["Name"].ToString(),
City = sdr["City"].ToString()
});
}
con.Close();
return employees;
}
}
}
}
}
View
In View, I' ll need
to import the namespace for accessing the Model classes.
Then we need to
declare the Model for the View as dynamically.
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>
</tr>
@foreach (Customer customer in Model.Customers)
{
<tr>
<td>@customer.CustomerId</td>
<td>@customer.CustomerName</td>
<td>@customer.City</td>
</tr>
}
</table>
<hr/>
<table cellpadding="0" cellspacing="0">
<tr>
<th>EmployeeID</th>
<th>Employee Name</th>
<th>City</th>
</tr>
@foreach (Employee employee in Model.Employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.EmployeeName</td>
<td>@employee.City</td>
</tr>
}
</table>
</body>
</html>
Note: For More details visit-
https://www.aspsnippets.com/Articles/Pass-Send-Multiple-Models-to-one-View-in-ASPNet-MVC-Razor.aspx
https://www.aspsnippets.com/Articles/Pass-Send-Multiple-Models-to-one-View-in-ASPNet-MVC-Razor.aspx
No comments:
Post a Comment