解决方案:传递到字典中的模型项的类型为'System.Collections.Generic.List`1 []',但此字典需要类型为'System.Collections.Generic.IEnumerable`1 []'的模型项 [英] Solution for: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[]'
问题描述
大家好,
这是我在MVC4应用程序中定义的模型:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Collections;
命名空间 BindPublicationsData.Models
{
public 类发布
{
public Int64 id { get ; set ; }
public string title { get 跨度>; set ; }
public string abstractInfo { get 跨度>; set ; }
public string description { get 跨度>; set ; }
public string authors { get 跨度>; set ; }
public string 理由{ get 跨度>; set ; }
public string keyTakeways { get 跨度>; set ; }
public string thumbnail { get 跨度>; set ; }
}
}
以下是Controller类:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
命名空间 BindPublicationsData.Controllers
{
public class PublicationController:Controller
{
private KnowledgeManagerEntities db = new KnowledgeManagerEntities();
public ActionResult Index()
{
var publicationsList =( from eachPub in db.KM_Publication select eachPub)。ToList();
return 查看(db.KM_Publication.ToList());
}
}
}
最后下面是我的观点:
@ model IEnumerable< BindPublicationsData.Models.Publication>
@ {
ViewBag.Title =Index;
}
< h2>出版物< / h2>
< table>
@foreach(模型中的var项目){
< tr>
< td>
@ Html.DisplayFor(modelItem => item.title)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.description)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.abstractInfo)
< / td>
< / tr>
}
< / table>
运行此应用程序后我我收到以下错误:
传入字典的模型项的类型为'System.Collections.Generic.List`1 [BindPublicationsData.KM_Publication] ',但是这个字典需要一个'System.Collections.Generic.IEnumerable`1 [BindPublicationsData.Models.Publication]'类型的模型项。
谁能告诉我为什么会收到这个错误?
这一行:
return 查看(db.KM_Publication.ToList());
将List元素传递给视图,但是查看您被声明为数据(模型)的IEnumerable ...
@ model IEnumerable < < span class =code-leadattribute> bindpublicationsdata.models.publication > < / bindpublicationsdata.models.publication >
您必须更改其中一个...例如
@ model List < bindpublicationsdata.models.publication > < / bindpublicationsdata.models.publication >
Hi all,
Here is my Model defined in MVC4 application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
namespace BindPublicationsData.Models
{
public class Publication
{
public Int64 id { get; set; }
public string title { get; set; }
public string abstractInfo { get; set; }
public string description { get; set; }
public string authors { get; set; }
public string rationale { get; set; }
public string keyTakeways { get; set; }
public string thumbnail { get; set; }
}
}
Below is the Controller class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace BindPublicationsData.Controllers
{
public class PublicationController : Controller
{
private KnowledgeManagerEntities db = new KnowledgeManagerEntities();
public ActionResult Index()
{
var publicationsList = (from eachPub in db.KM_Publication select eachPub).ToList();
return View(db.KM_Publication.ToList());
}
}
}
And finally below is my View:
@model IEnumerable<BindPublicationsData.Models.Publication>
@{
ViewBag.Title = "Index";
}
<h2>Publication</h2>
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
<td>
@Html.DisplayFor(modelItem => item.abstractInfo)
</td>
</tr>
}
</table>
After running this application I am getting the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[BindPublicationsData.KM_Publication]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[BindPublicationsData.Models.Publication]'.
Can any one tell me why I am getting this error?
This line:
return View(db.KM_Publication.ToList());
Passes a List element to the view, but in the view you are declared the data (model) a IEnumerable...
@model IEnumerable<bindpublicationsdata.models.publication></bindpublicationsdata.models.publication>
You have to change one of them...like
@model List<bindpublicationsdata.models.publication></bindpublicationsdata.models.publication>
这篇关于解决方案:传递到字典中的模型项的类型为'System.Collections.Generic.List`1 []',但此字典需要类型为'System.Collections.Generic.IEnumerable`1 []'的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!