需要类型为 'System.Collections.Generic.IEnumerable`1 的模型项
Requires a model item of type 'System.Collections.Generic.IEnumerable`1
我的控制器中有一个 LINQ 查询,它有一个选择所有记录的连接。然后我将 ReportCompletionStatus.AsEnumerable() 模型传递给我的视图。但我一直收到 fowling 异常..
传入字典的模型项的类型为'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 class(因为您的投影具有来自 Report_Completion_Status
和 Report_Category
的数据)并将其用于投影和视图的模型。
class
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
is not necessary.
下面是我如何让它工作的。
型号
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>
我的控制器中有一个 LINQ 查询,它有一个选择所有记录的连接。然后我将 ReportCompletionStatus.AsEnumerable() 模型传递给我的视图。但我一直收到 fowling 异常..
传入字典的模型项的类型为'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 class(因为您的投影具有来自 Report_Completion_Status
和 Report_Category
的数据)并将其用于投影和视图的模型。
class
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
is not necessary.
下面是我如何让它工作的。
型号
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>