简体   繁体   English

传递到字典中的模型项在ASP.net MVC中的类型为“ System.Collections.Generic.List…”

[英]The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC

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.List 1[<>f__AnonymousType1 6..... but this dictionary....System.Collections.Generic.IEnumerable`1. 我从ASP.net MVC实体框架中的两个表连接后检索数据,但是在视图内部无法访问属性,并且当我运行代码时出现错误:传递到字典中的模型项的类型为'System.Collections.Generic。列出1[<>f__AnonymousType1 6 .....但是这本字典.... 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 @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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net MVC“传入字典的模型项是&#39;System.Collections.Generic.List&#39;类型” - ASP.Net MVC “The model item passed into the dictionary is of type 'System.Collections.Generic.List” 在构建 ASP.NET Core Web 应用程序时,我得到“传递到字典中的模型项的类型为‘System.Collections.Generic.List`1” - While building ASP.NET Core web app I get "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1" 传递到字典中的模型项的类型为“ System.Collections.Generic.List” - The model item passed into the dictionary is of type 'System.Collections.Generic.List` 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List,但是此字典需要类型为的模型项 - model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List`1 [System.String] - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String] 异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“ System.Collections.Generic.List”。 - Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List` 传递到 ViewDataDictionary 的 model 项的类型为“System.Collections.Generic.List” - The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List` 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List`1 [TipoDeCanal]&#39;, - The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[TipoDeCanal]', 字典需要System.Collections.Generic.List类型的模型项 - Dictionary requires a model item of type System.Collections.Generic.List 传入字典的 model 项的类型为 'System.Collections.Generic.List,如何处理? - The model item passed into the dictionary is of type 'System.Collections.Generic.List, how to handel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM