![](/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.