首页 文章

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

提问于
浏览
0

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

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 回答

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

    多谢你们 :)

相关问题