Click here to Skip to main content
15,885,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am doing MVC project there I am getting below error. Please help to resolve.

HTML
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType1`5[System.Int32,System.String,System.String,System.DateTime,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[package.Models.EventsModel]'. 


My controller code is

C#
public ActionResult Index()
        {
            var Q = from a in _ObjectEntities.Events
                     join b in _ObjectEntities.Employees on a.Incharge equals b.Id
                     select new {
                         EventId = a.EventId,
                         EventName = a.EventName,
                         EventType = a.EventType,
                         EventDate = a.EventDate,
                         Incharge = b.Name
                     };

            
            return View(Q.AsEnumerable());
        }


Page header is:

<%@ Page Title="Events" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ToPSCompilers.Models.EventsModel>>" %>


Model:

C#
public class EventsModel
    {
        public int EventId { get; set; }
        public String EventName { get; set; }
        public String EventType { get; set; }
        public DateTime EventDate { get; set; }
        public String Incharge { get; set; }
    }



I checked with Google but no use. I am using MVC2 project with Visual studio 2010
Posted
Updated 17-Sep-14 20:08pm
v2

1 solution

your code:
C#
public ActionResult Index()
        {
            var Q = from a in _ObjectEntities.Events
                     join b in _ObjectEntities.Employees on a.Incharge equals b.Id
                     select new {
                         EventId = a.EventId,
                         EventName = a.EventName,
                         EventType = a.EventType,
                         EventDate = a.EventDate,
                         Incharge = b.Name
                     };
 
            
            return View(Q.AsEnumerable());
        }


Change to this:

C#
public ActionResult Index()
        {
            var Q = from a in _ObjectEntities.Events
                     join b in _ObjectEntities.Employees on a.Incharge equals b.Id
                     select new EventsModel {
                         EventId = a.EventId,
                         EventName = a.EventName,
                         EventType = a.EventType,
                         EventDate = a.EventDate,
                         Incharge = b.Name
                     };
 
            
            return View(Q.AsEnumerable());
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900