The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

asp.net-mvc, c#

Solution

As Shad points out, the reason you are getting this error is simply because you are passing a different type of data into the view than what you have said you would.

To solve this, you need a model that you can pass into your view that holds the data you need:

public class FooBarModel
{
    public AbortReason Foo { get;set;}
    public Activity Bar { get;set;}
}

Then, in your `Index` method, do something like this:

using(MobilePhoneXchangeEntities1 ent = new MobilePhoneXchangeEntities1())
{
    var result = from foo in ent.t_AbortReason
                 from bar in ent.t_Activity
                 where foo.AbortReasonCategoryId != null && bar.ActivityId != null
                 select new FooBarModel() { Foo = foo, Bar = bar };
    return View(result);
}

And in your view, set the model type:

@model IEnumerable<my.namespace.FooBarModel>

Problem

I been stuck in a situation and I tried finding the solution for it on net but was not successful. I am new to MVC with Entity Framework, and it is throwing the exception when i try to run the application: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType1`2[UnRelatedEntity.Models.t_AbortReason,UnRelatedEntity.Models.t_Activity]]', but this dictionary requires a model item of type 'UnRelatedEntity.Models.MobilePhoneXchangeEntities1' I am using an Entity as a model which fetches Data from two tables seperately which do not have relation among them. Controller: ``` public ActionResult Index() { MobilePhoneXchangeEntities1 ent = new MobilePhoneXchangeEntities1(); var result = from foo in ent.t_AbortReason from bar in ent.t_Activity where foo.AbortReasonCategoryId != null && bar.ActivityId != null select new { Foo = foo, Bar = bar }; return View(result); } ``` View ``` @model UnRelatedEntity.Models.MobilePhoneXchangeEntities1 ``` In the view I am just writing the above line i mean i am just inheriting the Model, nothing else but still I am confused about how to type cast model w.r.t model, but I am helpless. Can anyone please provide me help on this, but please keep in mind i am using two unrelated tables in my model.

Original source