这是DataController类:
[HttpGet]
public JsonResult EmployeeRequests()
{
List<EmployeeRequests> CO = new List<EmployeeRequests>();
using (EmployeeContext dc = new EmployeeContext())
{
//var cust = dc.Employees.OrderBy(a => a.EmpName).ToList();
var cust = dc.Employees.OrderBy(a => a.EmpName).ToList();
foreach (var i in cust)
{
var requests = dc.Requests.Where(a => a.EmployeeID.Equals(i.EmployeeID)).OrderBy(a => a.RequestDate).ToList();
CO.Add(new EmployeeRequests
{
Employee = i,
Requests = requests
});
}
}
return new JsonResult { Data = CO, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
一旦我删除它执行的表之间的关系,就无法使用关系在MVC4中加载嵌套数据。
我正在使用2个表Employee和Requests。