MVC System.InvalidOperationException:但是此字典需要类型为“System.Collections.Generic.IEnumerable”1[smoethng] 的模型项
Selam,
public ActionResult Edit()
{
var model = new HomeViewModel();
using (var context = new BannerEntities())
{
model.FotoList = context.Fotos.ToList();
return View(model);
}
}
我的观点是
@model IEnumerable<Banner.Models.HomeViewModel>
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
我犯了错误: 传递到字典中的模型项的类型为“Banner.Models.HomeViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Banner.Models.HomeViewModel]”的模型项。 知道为什么会出现这个问题吗?
Selam,
public ActionResult Edit()
{
var model = new HomeViewModel();
using (var context = new BannerEntities())
{
model.FotoList = context.Fotos.ToList();
return View(model);
}
}
And the view is
@model IEnumerable<Banner.Models.HomeViewModel>
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
I am taking the error:
The model item passed into the dictionary is of type 'Banner.Models.HomeViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Banner.Models.HomeViewModel]'.
Is there any idea why is that problem occure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在传递
Banner.Models.HomeViewModel
的单个实例,但您的视图需要Banner.Models.HomeViewModel
的IEnumerable
集合。将您的视图更改为如下所示,它应该可以工作:
You are passing a single instance of
Banner.Models.HomeViewModel
, but your view is expecting anIEnumerable
collection ofBanner.Models.HomeViewModel
.Change your view to look like this and it should work: