Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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></th>
    </tr>
    
    @foreach (var item in Model.Orders.Distinct<Order>())
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Customer.ContactName)
            </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);
        }



What i want is in the Customers section in the view to display DISTINCT customers of every employee ,but i cant do that , i dont know how:

@foreach (var item in Model.Orders.Distinct<Order>())
   {


What I have tried:

I just explained the problem above
Posted
Comments
CHill60 25-May-17 10:46am    
Surely it's in the Model that you would do that, or at worst the Controller. Definitely not the View.

Change the query from your earlier question to
Select DISTINCT E.LastName, E.FirstName, E.Title --,and some other employees fields
FROM Orders O
INNER JOIN Employees E ON O.EmployeeID = E.EmployeeID
ddgjgj 25-May-17 10:51am    
Sir , but is displays the list of customers in this way too, for every employee , what do you think , is it not OK this way ?

where to insert now this query in 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);
}
CHill60 25-May-17 11:19am    
1. That query does not display ANY customer information.
2. I told you on your previous post JOIN QUERY between three tables[^] that the query needs to go in the Model not the Controller.
3. See the comment from F-ES Sitecore below.
4. If you reply to a comment use the  Reply  button so that the poster is notified of your response
ddgjgj 26-May-17 4:23am    
TO BE EVERYTHING CLEAR , I JUST POSTED A NEW QUESTION HERE :

https://www.codeproject.com/Questions/1189186/Many-to-many-relationship-in-MVC-view
F-ES Sitecore 25-May-17 11:10am    
Just to elaborate slightly on what CHill60 has said, the model you are using (Employee) isn't fit for the purpose you want to use it so you need to do something else. Define your own model class that has an Employee object and also a List of Customer objects (the model can be an employee with a list as a property of it, or the model can contain both an Employee property and a List<Customer> property). You then get the model to construct itself from your database Employee class and the model will extract all the Employee info it needs, and then get a distinct list of customers from the Orders collection). So all your controller then does is something like

MyModel model = new MyModel(id);

and MyModel's constructor will get the relevant Employee from the database and populate the properties accordingly so your view will then simply use

Model.Employee

and

Model.Customers

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