传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [AppModels.Models.AppDto]',但此字典需要类型为'AppModels.Models.AppDto的模型项

人气:878 发布:2022-09-22 标签: ASP.NET4 MVC4

问题描述

当我使用我的模型值连接到数据库时,会出现此错误。任何人都可以帮助我 这里是我的代码: 公开覆盖列表< appdto>预约名单(int id) { List< appdto> dto = new List< appdto>(); List< appointment> db = _db.Appointments.Where(p => p.DID == id).ToList(); foreach(db中的var s) { var app = new AppDto(); app.PID = s.PID; app.Title = s.Title; app.Length = s.Length; app.AppDate = s.AppDate; dto.Add(app) ; } 返回dto; }

解决方案

你可以尝试使用@model更改为@model IEnumerable< your model => 绑定当前模型的地方。

When i am connecting to the database with my model values then this error is getting. Can any one help me out here is my code: public override List<appdto> Appointmentlist(int id) { List<appdto> dto = new List<appdto>(); List<appointment> db = _db.Appointments.Where(p => p.DID==id).ToList(); foreach (var s in db) { var app = new AppDto(); app.PID = s.PID; app.Title = s.Title; app.Length = s.Length; app.AppDate = s.AppDate; dto.Add(app); } return dto; }

解决方案

can you try to change in view using @model to @model IEnumerable<your model=""> where you bind your current model.

753