2011-03-04 15 views
0

我想從課程表中的項目(具有CourseId)和相關CourseName中獲取數據。使用linq從兩個表(加入)中獲取數據並將結果返回到視圖

我已經寫了下面的代碼:

var projects = from n in db.Projects 
         join c in db.Courses on n.CourseId equals c.ID 
         orderby n.Name 
         select new { Project = n, Course = c }; 

     return View(projects.ToList()); 

,我得到錯誤:

The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[<>f__AnonymousType2 2[ProjectManager.Models.Project,ProjectManager.Models.Course]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ProjectManager.Models.Project]'.

我需要在控制器做在查看以顯示這些數據?

回答

0

您正在向您的視圖中傳遞匿名類型,但視圖聲明爲IEnumerable<Project>

您應該從視圖中刪除@model聲明,以使其使用dynamic模型。

相關問題