首页 > 解决方案 > Web 应用程序表

问题描述

我正在开发一个 Web 应用程序项目,在我的模型中我有两个列表:

 public IEnumerable<Employee> EmployeeList { get; set; }
 public List<int> AnotherInfo { get; set; }

在我的视图模型中,我将 Employee 列表中的项目显示为表格:

 @foreach (var employee in Model.EmployeeList)
  {
     <tr>
         <td>@employee.Names</td>
         <td>@employee.Age</td>
         <td>@@employee.Address</td>               
     </tr>
 }

结果,我有一个包含 3 列的表:姓名、年龄和地址。

现在我想要第四列显示第二个列表:AnotherInfo

有什么办法可以使第四列与其他列位于同一行?

标签: c#htmlrazor-pages

解决方案


您可以尝试解析EmployeeList为列表并使用循环进行for循环,如下所示:

public List<Employee> EmployeeList { get; set; }
public List<int> AnotherInfo { get; set; }

@for (var index = 0; index < EmployeeList.Count; index++)
{
    var employee = EmployeeList[index];
    var info = AnotherInfo[index];
    <tr>
        <td>@employee.Names</td>
        <td>@employee.Age</td>
        <td>@employee.Address</td>               
        <td>@info</td>
    </tr>
}

更新:

如果你不能改变 的类型EmployeeList,你仍然可以根据它制作一个副本列表:

List<Employee> list = EmployeeList.ToList();
// or
// var list = EmployeeList.ToList();

然后,您尝试上面的解决方案。


推荐阅读