本文介绍了传递到字典的模型产品类型“System.Data.Entity.Infrastructure.DbQuery',但这需要字典B型的模型项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直停留在一个情况下,我试图寻找净它的解决方案,但没有成功。我是新来的MVC实体框架,它是抛出异常,当我尝试运行应用程序:

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:

传递到字典中的模型产品类型
  'System.Data.Entity.Infrastructure.DbQuery<$c$c>1[<>f__AnonymousType12[UnRelatedEntity.Models.t_AbortReason,UnRelatedEntity.Models.t_Activity]]',
  但本词典需要类型的模型项目
  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.

控制器:

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);
}

查看

@model UnRelatedEntity.Models.MobilePhoneXchangeEntities1

在认为我只是写了上线我的意思是我只是继承模型,闲来无事但我仍然感到困惑如何强制类型转换模型w.r.t模式,但我也很无奈。

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.

推荐答案

由于沙德指出的,你收到此错误的原因很简单,因为你是通过不同类型的数据转换成比你说你会认为

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;}
}

然后,在你的首页方法,做这样的事情:

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>

这篇关于传递到字典的模型产品类型“System.Data.Entity.Infrastructure.DbQuery',但这需要字典B型的模型项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:22