如何为每个部门名称创建多个html表并动态显示员工详细信息?

时间:2018-11-22 13:57:18

标签: asp.net-mvc

我有两个表,一个是员工,一个是部门,我想为每个部门名称创建html表,并动态显示特定部门的员工详细信息 我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果您在服务器端使用实体框架并在表之间建立联系,则可以使用以下代码

public class Departments
{
    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }
    public List<Employee> EmployeeList { get; set; }
}

public class Employee
{
    public int DepartmentID { get; set; }
    public int EmployeeID { get; set; }
    public string EmployeeName { get; set; }
}

public ActionResult DepartmentList()
{
    List<Departments> deptList = GetDepartments();
        List<Employee> empList = GetEmployee();

        foreach (Departments dept in deptList)
        {
            dept.EmployeeList = empList.Where(x => x.DepartmentID == dept.DepartmentID).ToList();
        }

        return View(deptList);
}

.cshtml页面

@model IEnumerable<WebApplication1.Controllers.Departments>

...

<div>
    @foreach (var item in Model)
    {
        <div>
            @item.DepartmentName
        </div>
        <div>
            <table>
                <tbody>
                    @foreach (var emp in item.EmployeeList)
                    {
                        <tr>
                            <td>
                                @emp.EmployeeID
                            </td>
                            <td>
                                @emp.EmployeeName
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    }
</div>

这可以帮助您

谢谢