我正在尝试使用 MVC3 Razor 中的 LINQ 从数据库中选择多个表。为了在视图中查看多个表中的多个数据,我需要对我的代码进行哪些更改?
这是我的模特
public class DailyAttendance
{
[Key]
public string pnp { get; set; }
public string student_id { get; set; }
public string mth_yr_id { get; set; }
public string D01 { get; set; }
public string D02 { get; set; }
} // and so on
public class TitasDb : DbContext
{
public DbSet<DailyAttendance> DailyAttendance { get; set; }
public DbSet<MonthYear> MonthYear { get; set; }
// and so on
}
这就是我在控制器中使用 LINQ 的方式
public class AttendanceController : Controller
{
TitasDb _attendanceModel = new TitasDb();
public ActionResult Index()
{
var model = from s in _attendanceModel.Student
join ssc in _attendanceModel.StudentSchoolClass on s.student_id equals ssc.student_id
join sc in _attendanceModel.SchoolClass on ssc.school_class_id equals sc.school_class_id
join da in _attendanceModel.DailyAttendance on s.student_id equals da.student_id
join my in _attendanceModel.MonthYear on da.mth_yr_id equals my.mth_yr_id
where my.month_id == "M05"
where sc.teacher_id == "T1000001"
select new { DailyAttendance = da, Student = s };
return View(model);
}
}
这就是我尝试查看数据的方式
@model IEnumerable<titas.Models.DailyAttendance>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.student_id)
<td>
</tr>
}
我得到的错误是
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType5`2[titas.Models.DailyAttendance,titas.Models.Student]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[titas.Models.DailyAttendance]'.