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

Asp.net mvc 如何创建多个html表并动态显示每个部门名称的员工详细信息?,asp.net-mvc,Asp.net Mvc,我有两个表,一个是employees,一个是department,我想为每个部门名称创建html表,并动态显示特定部门的员工详细信息 我怎么能做到 若您在服务器端使用实体框架,并且表之间有关系,那个么您可以使用如下代码 public class Departments { public int DepartmentID { get; set; } public string DepartmentName { get; set; } public List<Emplo

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

若您在服务器端使用实体框架,并且表之间有关系,那个么您可以使用如下代码

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);
}
公共类部门
{
public int DepartmentID{get;set;}
公共字符串DepartmentName{get;set;}
公共列表EmployeeList{get;set;}
}
公营雇员
{
public int DepartmentID{get;set;}
public int EmployeeID{get;set;}
公共字符串EmployeeName{get;set;}
}
公共行动结果部门列表()
{
List deptList=GetDepartments();
List employee=GetEmployee();
foreach(部门列表中的部门)
{
dept.EmployeeList=employist.Where(x=>x.DepartmentID==dept.DepartmentID.ToList();
}
返回视图(部门列表);
}
和.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>
@model IEnumerable
...
@foreach(模型中的var项目)
{
@item.DepartmentName
@foreach(项目中的var emp.EmployeeList)
{
@emp.EmployeeID
@emp.EmployeeName
}
}
这对你有帮助吗


谢谢

我正在使用Linq to Sql