0

I have a LINQ query in my controller that has a join which selects all records. I'm then passing the ReportCompletionStatus.AsEnumerable() model to my view. But I keep getting the fowlling exceptions..

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1

but this dictionary requires a model item of type '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...

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>
GRU119
  • 1,028
  • 1
  • 14
  • 31
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. –  Aug 19 '16 at 23:38

2 Answers2

2

With your select new, you project to an anonymous type, not to an 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.

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.

Community
  • 1
  • 1
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Thanks @Raphaël Althaus for your answer. I have created the new ViewModel class as you described, but in the projection poriton of the code, i'm now getting "Cannot initialize type 'SomeViewModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'" – GRU119 Aug 19 '16 at 16:52
0

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>
GRU119
  • 1,028
  • 1
  • 14
  • 31