use*_*519 1 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
我从ASP.net MVC实体框架中的两个表加入后检索数据但是在视图中无法访问属性,当我运行代码时它给出错误:传递到字典中的模型项是'System.Collections.Generic类型.列表1[<>f__AnonymousType16 .....但这个字典.... System.Collections.Generic.IEnumerable`1.谢谢你的回答..
我的代码是:
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());
}
Run Code Online (Sandbox Code Playgroud)
在视图中:
@model IEnumerable
@foreach (var item in Model)
{
@item.... (Properties are inaccessible)
}
Run Code Online (Sandbox Code Playgroud)
剃刀视图不支持使用匿名类型.
您应该创建一个可以填充的模型.在模型中添加所需的所有属性.
代替
select new { }
Run Code Online (Sandbox Code Playgroud)
你会去的
select new MyModel { }
Run Code Online (Sandbox Code Playgroud)
查询将返回一个
List<MyModel>
Run Code Online (Sandbox Code Playgroud)
经过这些改变.
然后在您的视图中,您将模型更改为:
@model IEnumerable<MyModel>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23250 次 |
| 最近记录: |