![](/img/trans.png)
[英]The model item passed is of type 'System.Collections.Generic.List but this dictionary requires 'System.Collections.Generic.IEnumerable
[英]Requires a model item of type 'System.Collections.Generic.IEnumerable`1
我的控制器中有一個LINQ查詢,該查詢具有一個選擇所有記錄的聯接。 然后,我將ReportCompletionStatus.AsEnumerable()模型傳遞給我的視圖。 但是我不斷收到令人毛骨悚然的異常。
傳遞到字典中的模型項的類型為'System.Data.Entity.Infrastructure.DbQuery`1
但是此字典需要類型為'System.Collections.Generic.IEnumerable`1的模型項
我正在設置模型AsEnumerable(),並且我的視圖期望使用@model IEnumerable,所以我仍然不確定為什么它會產生抱怨...
控制者
var ReportCompletionStatus = from r in db.Report_Completion_Status
join rc in db.Report_Category
on r.Report_Category equals rc.ReportCategoryID
select new
{
r.Report_Num,
rc.ReportCategory,
r.Report_Sub_Category,
r.Report_Name,
r.Report_Owner,
r.Report_Link,
r.Report_Description,
r.Last_Published,
r.Previous_Published,
r.Published_By,
r.Previous_Published_By,
r.Last_Edited,
r.Edited_By
};
return View(ReportCompletionStatus.AsEnumerable());
模型
@model IEnumerable<WebReportingTool.Report_Completion_Status>
使用select new
,您將投影到匿名類型,而不是IEnumerable<WebReportingTool.Report_Completion_Status>
您需要創建一個ViewModel類(因為您的投影同時具有Report_Completion_Status
和Report_Category
數據),並將其用於投影和View的模型。
類
public class SomeViewModel {
public int ReportNum {get;set;}
public string ReportCategory {get;set;
//etc.
}
投影
select new SomeViewModel
{
ReportNum = r.Report_Num,
ReportCategory = rc.ReportCategory,
//etc.
};
視圖
@model IEnumerable<SomeViewModel>
順便說一下, AsEnumerable
不是必需的 。
這是我如何使其工作的方法。
模型
public class ReportCategoryListModel
{
public int Report_Num { get; set; }
public string ReportCategory { get; set; }
public string Report_Sub_Category { get; set; }
public string Report_Name { get; set; }
public string Report_Owner { get; set; }
public string Report_Link { get; set; }
public string Report_Description { get; set; }
public Nullable<System.DateTime> Last_Published { get; set; }
public Nullable<System.DateTime> Previous_Published { get; set; }
public Nullable<int> Published_By { get; set; }
public Nullable<int> Previous_Published_By { get; set; }
public Nullable<System.DateTime> Last_Edited { get; set; }
public Nullable<int> Edited_By { get; set; }
}
控制者
var ReportCompletionStatus = from r in db.Report_Completion_Status
join rc in db.Report_Category
on r.Report_Category equals rc.ReportCategoryID
select new ReportCategoryListModel
{
Report_Num = r.Report_Num,
ReportCategory = rc.ReportCategory,
Report_Sub_Category = r.Report_Sub_Category,
Report_Name = r.Report_Name,
Report_Owner = r.Report_Owner,
Report_Link = r.Report_Link,
Report_Description = r.Report_Description,
Last_Published = r.Last_Published,
Previous_Published= r.Previous_Published,
Published_By = r.Published_By,
Previous_Published_By = r.Previous_Published_By,
Last_Edited = r.Last_Edited,
Edited_By = r.Edited_By
};
return View(ReportCompletionStatus);
視圖
@model IEnumerable<WebReportingTool.Models.ReportCategoryListModel>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.