我如何从两个表中发送数据列表来查看?

时间:2016-04-18 10:04:06

标签: asp.net-mvc entity-framework-6 ef-database-first

我首先在asp mvc应用程序数据库中工作,所以我有两个表Employee和Branch,我需要发送列表来查看员工姓名和分支名称,这是我的控制器代码

 public PartialViewResult List()
    {
        List<Employee> emp;
        using (var contxt = new EnglisCenterEntities())
        {

            var query = contxt.Employee.ToList();
            emp = query;
        }
        return PartialView(emp);
    }

我的观看代码是

@model IEnumerable<Insert.Models.Employee>

         <p>
      @Html.ActionLink("Create New", "Create")
         </p>
  <table class="table">
      <tr>

         <th>
        @Html.DisplayNameFor(model => model.EmpName)
        </th>
       <th>
        @Html.DisplayNameFor(model => model.Phone)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Username)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Branches.BranchName)
    </th>
    <th></th>
</tr>

     @foreach (var item in Model) {
   <tr>

    <td>
        @Html.DisplayFor(modelItem => item.EmpName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Phone)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Username)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Branches.BranchName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.IdEmp }) |
        @Html.ActionLink("Details", "Details", new { id=item.IdEmp }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.IdEmp })
    </td>
</tr>

}

但它不起作用,所以我如何发送分支名称与员工数据

1 个答案:

答案 0 :(得分:1)

这是:

  public PartialViewResult List()
    {
        List<Employee> emp;
        using (var contxt = new EnglisCenterEntities())
        {

            var brands = contxt.Employee.Include("Branches").ToList();
           // var query = contxt.Employee.ToList();
            emp = brands;
        }
        return PartialView(emp);
    }

谢谢你们:)