传递到字典的模型产品类型System.Collections.Generic.List的“......在ASP.net MVC [英] The model item passed into the dictionary is of type 'System.Collections.Generic.List... in ASP.net MVC

查看:1725
本文介绍了传递到字典的模型产品类型System.Collections.Generic.List的“......在ASP.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.net MVC框架的实体两个表加盟后检索数据,但性质是内部视图不可访问,当我运行code它给错误:传递到字典的模型项的类型是 System.Collections.Generic.List 1 [<> f__AnonymousType1 6 .....
但是这本词典.... System.Collections.Generic.IEnumerable`1。谢谢您的回答。

我的code是:

HomeController的:

  dbEntities的DBQuery =新dbEntities();        公众的ActionResult指数()
        {
            变种的id = 1;
            变种查询=(从dbquery.Emp_点¯x
                         加入的Y dbquery.Departments上x.Id等于y.emp_id
                         其中,x.Id == ID
                         选择新的{x.Id,x.EmployeeName,x.Department,y.emp_id,y.Dept_Id,y.DeptName})了ToList()。
            返回查看(query.ToList());        }

在View:

@model IEnumerable的

  @foreach(以型号VAR项)
  {
   @item ....(属性都无法访问)
  }


解决方案

使用匿名类型不被剃刀意见的支持。

您应该创建可以填充模式。添加您在模型中需要的所有属性。

而不是

 选择新{}

您会去

 选择新为MyModel {}

该查询将返回

 列表<&为MyModel GT;

这些更改后。

然后在你的观点,你会改变模式:

  @model IEnumerable的<&为MyModel GT;

I am retrieving data after joining from two tables in ASP.net MVC Entity framework but the properties are unaccessible inside view and when i run the code it give error : The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType16..... but this dictionary....System.Collections.Generic.IEnumerable`1. Thanks for your answers..

My Code is:

HomeController:

 dbEntities dbquery = new dbEntities();

        public ActionResult Index()
        {
            var id = 1;
            var query = (from x in dbquery.Emp_
                         join y in dbquery.Departments on x.Id equals y.emp_id
                         where x.Id == id
                         select new { x.Id, x.EmployeeName, x.Department, y.emp_id, y.Dept_Id, y.DeptName }).ToList(); 
            return View(query.ToList());

        }

In View:

@model IEnumerable

  @foreach (var item in Model)
  { 
   @item.... (Properties are inaccessible)
  }

解决方案

The use of anonymous types are not supported by razor views.

You should create a model you could populate. Add all the properties you need in the model.

Instead of

  select new { } 

you would go

  select new MyModel  { } 

The query would return a

    List<MyModel>

after these changes.

Then in your view you would change the model to:

     @model IEnumerable<MyModel>

这篇关于传递到字典的模型产品类型System.Collections.Generic.List的“......在ASP.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆