[英]Requires a model item of type 'System.Collections.Generic.IEnumerable`1
I have a LINQ query in my controller that has a join which selects all records. 我的控制器中有一个LINQ查询,该查询具有一个选择所有记录的联接。 I'm then passing the ReportCompletionStatus.AsEnumerable() model to my view. 然后,我将ReportCompletionStatus.AsEnumerable()模型传递给我的视图。 But I keep getting the fowlling exceptions.. 但是我不断收到令人毛骨悚然的异常。
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1 传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery`1
but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1 但是此字典需要类型为'System.Collections.Generic.IEnumerable`1的模型项
I'm setting the model AsEnumerable() and my view is expecting @model IEnumerable so i'm still not sure why it's complaning... 我正在设置模型AsEnumerable(),并且我的视图期望使用@model IEnumerable,所以我仍然不确定为什么它会产生抱怨...
Controller 控制者
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 模型
@model IEnumerable<WebReportingTool.Report_Completion_Status>
With your select new
, you project to an anonymous type, not to an IEnumerable<WebReportingTool.Report_Completion_Status>
使用select new
,您将投影到匿名类型,而不是IEnumerable<WebReportingTool.Report_Completion_Status>
You need to create a ViewModel class (as your projection has data from both Report_Completion_Status
and Report_Category
) and use it for projection and for your View's model. 您需要创建一个ViewModel类(因为您的投影同时具有Report_Completion_Status
和Report_Category
数据),并将其用于投影和View的模型。
class 类
public class SomeViewModel {
public int ReportNum {get;set;}
public string ReportCategory {get;set;
//etc.
}
projection 投影
select new SomeViewModel
{
ReportNum = r.Report_Num,
ReportCategory = rc.ReportCategory,
//etc.
};
view 视图
@model IEnumerable<SomeViewModel>
By the way, the AsEnumerable
is not necessary . 顺便说一下, AsEnumerable
不是必需的 。
Here's how I got it to work. 这是我如何使其工作的方法。
Model 模型
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; }
}
Controller 控制者
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);
View 视图
@model IEnumerable<WebReportingTool.Models.ReportCategoryListModel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.