繁体   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

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

我的代码是:

家庭控制器:

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

        }

在视图中:

@model IEnumerable

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

剃刀视图不支持使用匿名类型。

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

代替

  select new { } 

你会去

  select new MyModel  { } 

查询将返回

    List<MyModel>

这些更改之后。

然后在您看来,您可以将模型更改为:

     @model IEnumerable<MyModel>

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM