Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 :

// GET: Employees/Details/5
        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 :

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

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; }
    }
}







//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

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; }
    }
}








//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Northwind
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public partial class Customer
    {
      //  internal static readonly object ContactName;

        [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; }

        //     [StringLength(15, MinimumLength = 3, ErrorMessage = "Invalid")]
        //  [MaxLength(15), MinLength(5)]
       

        //  public virtual Shipper Shipper { 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; }

     //   [NotMapped]
       // public virtual Region Regionn { get; set; }
       public virtual Region Regionn { get; set; }
        public Nullable<int> RegionVia { get; set; }

        public virtual Employee Employee { get; set; }

        //    public string Region { get; set; }
        //  [NotMapped]
        //   public List<Region> RegionList { get; set; }

        //public Region Regionn { get; set; } // Navigation Property  

        //[NotMapped]
        //  public string RegionID { get; set; }

        //  [NotMapped]
        //     public List<Region> RegionList { get; set; }

        //   public int RegionID { 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 :

C#
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.
Posted
Updated 25-May-17 22:39pm
v2
Comments
CHill60 26-May-17 4:40am    
Where is the code that actually reads the data from the database?
ddgjgj 26-May-17 4:41am    
namespace Northwind.Controllers
{
public class EmployeesController : Controller
{
private dbNorthwindEntities db = new dbNorthwindEntities();

// GET: Employees
public ActionResult Index()
{
var employees = db.Employees;
// var employees = db.Employees.Include(e => e.Employee1).Include(e => e.Employees1);
// var employees = db.Employees.Include(e => e.Employees1);

// var employees = db.Employees.Include(e => e.Employee1);

return View(employees.ToList());
}



// GET: Employees/Details/5
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);
}
ddgjgj 26-May-17 5:01am    
at the Employee class , delete this
this.Customers = new HashSet<customer>(); and this
public virtual ICollection<customer> Customers { get; set; } , because these dont exist , i just added this for test , but its not right this way. And also at customer class delete these :
public int EmployeeID { get; set; } and
public virtual Employee Employee { get; set; } , they dont exist in database too.
ddgjgj 26-May-17 6:58am    
is it ok:

Employee employee = db.Employees.Include(player => player.Orders).Where(i => i.EmployeeID == id).SingleOrDefault();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900