我有一个由实体框架填充的预订类,我想针对表运行不同的查询并将它们都返回到视图中。我读过关于使用一个类来组合它们,但我无法让它工作......
谢谢你的帮助
错误
foreach 语句不能对“ITAPP.Models.Bookings”类型的变量进行操作,因为“ITAPP.Models.Bookings”不包含“GetEnumerator”的公共定义
班级
namespace ITAPP.Models
{
public class Bookings
{
public List<tblBooking> BookedIn { get; set; }
public List<tblBooking> BookedOut { get; set; }
}
}
用法(为了测试我对两者使用相同的查询,我将在之后使用输入/输出查询)
var tblBookings = from d in db.tblBookings.Include(t => t.Equipment).Include(t => t.tblUser)
where (d.Equipment.Bookable == true ) &&
(d.Equipment.Deleted == false) &&
(d.Equipment.DecommissionDate == null || d.Equipment.DecommissionDate == dateBlank1 || d.Equipment.DecommissionDate == dateBlank2)
select d;
Bookings Bookings = new Bookings();
Bookings.BookedIn = tblBookings.ToList();
Bookings.BookedOut = tblBookings.ToList();
return View("Loaned", Bookings);
看法
@model ITAPP.Models.Bookings
@foreach (var item in Model) {
foreach (var inItem in item.BookedIn)
{
<td>
@Html.DisplayFor(modelItem => inItem.tblUser.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => inItem.BookFrom)
</td>
<td>
@Html.DisplayFor(modelItem => inItem.BookTo)
</td>
}
}