[英]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.