This is the view :
@model Northwind.Employee
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/MasterDetailsLayoutPage.cshtml";
}
<h2>Details</h2>
<div>
<h4>Employee</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.TitleOfCourtesy)
</dt>
<dd>
@Html.DisplayFor(model => model.TitleOfCourtesy)
</dd>
<dt>
@Html.DisplayNameFor(model => model.BirthDate)
</dt>
<dd>
@Html.DisplayFor(model => model.BirthDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.HireDate)
</dt>
<dd>
@Html.DisplayFor(model => model.HireDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.City)
</dt>
<dd>
@Html.DisplayFor(model => model.City)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Region)
</dt>
<dd>
@Html.DisplayFor(model => model.Region)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PostalCode)
</dt>
<dd>
@Html.DisplayFor(model => model.PostalCode)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Country)
</dt>
<dd>
@Html.DisplayFor(model => model.Country)
</dd>
<dt>
@Html.DisplayNameFor(model => model.HomePhone)
</dt>
<dd>
@Html.DisplayFor(model => model.HomePhone)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Extension)
</dt>
<dd>
@Html.DisplayFor(model => model.Extension)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Photo)
</dt>
<dd>
@{
byte[] photo = Model.Photo;
string imageSrc = "image / jpeg";
if (photo != null)
{
MemoryStream ms = new MemoryStream();
ms.Write(photo, 78, photo.Length - 78);
string imageBase64 = Convert.ToBase64String(ms.ToArray());
imageSrc = string.Format("data:image/jpeg;base64,{0}", imageBase64);
}
}
<img src="@imageSrc" alt="Image" width="50" height="50"/>
</dd>
<dt>
@Html.DisplayNameFor(model => model.Notes)
</dt>
<dd>
@Html.DisplayFor(model => model.Notes)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PhotoPath)
</dt>
<dd>
@Html.DisplayFor(model => model.PhotoPath)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Employee1.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.Employee1.LastName)
</dd>
</dl>
</div>
<h4>Customers </h4>
<p>
@Html.ActionLink("Create New Customer", "Create", "Customers", new { id = Model.EmployeeID }, null)
</p>
<table class="table">
<tr>
<th>
Customer ID
</th>
<th>
Customer Name
</th>
<th>
Order ID
</th>
<th></th>
</tr>
@foreach (var item in Model.Orders)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Customer.ContactName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderID)
</td>
<td>
@Html.ActionLink("Editt ", "Edit", "Customers", new { id = item.CustomerID }, null)
@Html.ActionLink("Detailss ", "Details", "Customers", new { id = item.CustomerID }, null)
@Html.ActionLink("Deletee ", "Delete", "Customers", new { id = item.CustomerID }, null)
@Html.ActionLink("Edit", "Edit", new { id = item.CustomerID }) |
@Html.ActionLink("Details", "Details", new { id = item.CustomerID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CustomerID })
</td>
</tr>
}
</table>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.EmployeeID }) |
@Html.ActionLink("Back to List", "Index")
</p>
And this is the controller :
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
This is the Employee class and CUSTOMER CLASS AND ORDER CLASS :
namespace Northwind
{
using System;
using System.Collections.Generic;
public partial class Order
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Order()
{
this.Order_Details = new HashSet<Order_Detail>();
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public Nullable<int> EmployeeID { get; set; }
public Nullable<System.DateTime> OrderDate { get; set; }
public Nullable<System.DateTime> RequiredDate { get; set; }
public Nullable<System.DateTime> ShippedDate { get; set; }
public Nullable<int> ShipVia { get; set; }
public Nullable<decimal> Freight { get; set; }
public string ShipName { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public string ShipRegion { get; set; }
public string ShipPostalCode { get; set; }
public string ShipCountry { get; set; }
public virtual Customer Customer { get; set; }
public virtual Employee Employee { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Order_Detail> Order_Details { get; set; }
public virtual Shipper Shipper { get; set; }
}
}
namespace Northwind
{
using System;
using System.Collections.Generic;
public partial class Employee
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Employee()
{
this.Employees1 = new HashSet<Employee>();
this.Orders = new HashSet<Order>();
this.Territories = new HashSet<Territory>();
this.Customers = new HashSet<Customer>();
}
public int EmployeeID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Title { get; set; }
public string TitleOfCourtesy { get; set; }
public Nullable<System.DateTime> BirthDate { get; set; }
public Nullable<System.DateTime> HireDate { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string HomePhone { get; set; }
public string Extension { get; set; }
public byte[] Photo { get; set; }
public string Notes { get; set; }
public Nullable<int> ReportsTo { get; set; }
public string PhotoPath { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Employee> Employees1 { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
public virtual Employee Employee1 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Order> Orders { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Territory> Territories { get; set; }
}
}
namespace Northwind
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class Customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Customer()
{
this.Orders = new HashSet<Order>();
this.CustomerDemographics = new HashSet<CustomerDemographic>();
}
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Order> Orders { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CustomerDemographic> CustomerDemographics { get; set; }
public virtual Region Regionn { get; set; }
public Nullable<int> RegionVia { get; set; }
public virtual Employee Employee { get; set; }
}
}
So , I have three tables of Northwind database , Employee(employeeID) 1-* Order(Orderid,customerid,employeeid)*-1 Customer(customerID) . So it is a many to many relationship ,1 Employee has many Orders and 1 customer have many orders , so it is 1* *1 relationship , with an order table in the middle. The details view lists all the details info about a particular employee based on ID in the url , and under that it shows a table of ALL ITS CUSTOMERS . So , since the employee class dont have direct relation with customer class , it is a a many to many relationship in between : Employee has many Order and Customer has many Order . So how do i extract and display in a view all distinct customers of every employee.Im using Entity framework , and my entities are like this :
namespace Northwind.Controllers
{
public class EmployeesController : Controller
{
private dbNorthwindEntities db = new dbNorthwindEntities();
What I have tried:
I tried this :
@foreach (var item in Model.Orders)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Customer.ContactName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderID)
</td>
It works , but it loads very slow and a want another way of doing it.