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

前端 未结 5 1553
太阳男子
太阳男子 2020-12-18 13:16

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 wh

5条回答
  •  醉酒成梦
    2020-12-18 14:03

    Your view expects model of type UnRelatedEntity.Models.MobilePhoneXchangeEntities1, but you pass result object to it. result is a database query, which you have described in controller action. So, you get expected error - type mismatch.

    To fix this you should create type for your query items. Very simple example:

    public class ResultItem
    {
        public Foo Foo { get; set; }
        public Bar Bar { get; set; }
    }
    

    After that modify select statement in your query:

    select new ResultItem { Foo = foo, Bar = bar };
    

    and in the end, change model type of the view to IEnumerable, because query will return collection of result items.

提交回复
热议问题