我試圖從我的控制器傳遞一個LINQ列表對象到我的視圖。 linq對象包含引發某種錯誤的分組。我只想在視圖中顯示分組對象。 linq語句完美地工作,但顯示語句不!任何幫助將不勝感激!傳遞分組LINQ對象查看
控制器
public ViewResult StudentAttendanceForYear(int id)
{
DateTime finDate = System.DateTime.Today;
DateTime strtDate = DateTime.Today.AddMonths(-6);
var chosenStudent = (from t in db.ClassInstanceDetails.Include("Student")
where (t.Attendance == false) && (t.StudentID == id)
&& (t.ClassInstance.Date > strtDate) && (t.ClassInstance.Date < finDate)
group t by new { t.ClassInstance.Date.Year, t.ClassInstance.Date.Month, t.ClassInstance.Date.Day } into grp
select new
{
absentDate = grp.Key,
numAbsences = grp.Count(t => t.Attendance == false)
}).ToList();
return View(chosenStudent.ToList());
}
視圖
我試圖改變我的觀點,以
@model IEnumerable<System.Linq.IGrouping<object, FYPSchoolApp.DAL.ClassInstanceDetail>>
但仍沒有運氣,我不斷收到以下錯誤:
模型項目傳入字典的類型是'System.Collections.Generic.List 1[<>f__AnonymousType7
2 [<> f__AnonymousType6 3[System.Int32,System.Int32,System.Int32],System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [System.Linq.IGrouping`2 [System.Object,FYPSchoolApp.DAL.ClassInstanceDetail]]'。
非常感謝您的回覆,我對這一切都比較陌生!我創建了VIewModel並輸入了你的代碼,但是現在我得到這個錯誤「LINQ to Entities只支持無參數的構造函數和初始化器。」任何想法如何解決它? – 2013-02-19 21:24:35
對不起,這是由'新日期時間(...'造成的,並有多種方法可以解決它...我會更新我的答案。 – nemesv 2013-02-19 21:29:47
修復它!非常非常非常!它可以安全地說我永遠不會已經解決了我自己! – 2013-02-19 21:49:55