如何在一个视图上显示两个表

时间:2016-07-13 12:34:06

标签: asp.net asp.net-mvc-4

模型

public class aaa
{
   public List<emp> emps { set; get; }
   public List<dept> depts { set; get; }  
}

行动方法

public ActionResult detail() 
{ 
   aaa a = new aaa(); 
   a.emps = (from p in db.emps select p).ToList(); 
   a.depts = (from p in db.depts select p).ToList(); 
   return View(a); 
}

1 个答案:

答案 0 :(得分:1)

查看 中的模型应为@model data.Models.aaa

@model data.Models.aaa

<table>
    @foreach (var emp in Model.emps)
    {
        <tr>
            <td>@emp.FirstName</td>
        </tr> 
    }
</table>
<table>
    @foreach (var dept in Model.depts)
    {
        <tr>
            <td>@dept.Title</td>
        </tr> 
    }
</table>