需要类型为' System.Collections.Generic.IEnumerable`1的模型项

时间:2016-08-19 14:55:05

标签: asp.net-mvc linq ienumerable

我的控制器中有一个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>

2 个答案:

答案 0 :(得分:2)

使用select new,您投射到匿名类型,而不投射到IEnumerable<WebReportingTool.Report_Completion_Status>

您需要创建一个ViewModel类(因为您的投影包含来自Report_Completion_StatusReport_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 is not necessary

答案 1 :(得分:0)

以下是我如何使用它。

<强>模型

  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>