You return an anonymous object, while your presentation is strongly typed on IEnumerable<mvc4application.models.employee> .
I highly recommend you write a presentation model that will meet the requirements of your presentation and contain the information you would like to work with in this view:
public class EmployeeViewModel { public int EmployeeID { get; set; } public string Name { get; set; } public string ManagerName { get; set; } public string Designation { get; set; } public string Phone { get; set; } public string Address { get; set; } }
and then adapt your LINQ query to project the object's EF object into the view model:
IEnumerable<EmployeeViewModel> employees = from m in t.Employees join e1 in t.Employees on m.ManagerID equals e1.EmployeeID select new EmployeeViewModel { EmployeeID = m.EmployeeID , Name = m.Name, ManagerName = e1.Name, Designation = m.Designation, Phone = m.Phone, Address = m.Address }; return View(employees.ToList());
and finally make your view strongly typed for the view model:
@model IList<EmployeeViewModel>
and now you can provide information:
<table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Manager name</th> <th>Designation</th> <th>Phone</th> <th>Address</th> </tr> </thead> <tbody> @for (var i = 0; i < Model.Count; i++) { <tr> <td>@Html.DisplayFor(x => x[i].EmployeeID)</td> <td>@Html.DisplayFor(x => x[i].Name)</td> <td>@Html.DisplayFor(x => x[i].ManagerName)</td> <td>@Html.DisplayFor(x => x[i].Designation)</td> <td>@Html.DisplayFor(x => x[i].Phone)</td> <td>@Html.DisplayFor(x => x[i].Address)</td> </tr> } </tbody> </table>
source share