但本词典需要类型的“典范项目System.Collections.Generic.IEnumerable`1 [英] but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1
问题描述
我得到这个错误,我不知道如果我能做到这一点,这里是我的代码..
应用程序控制器
公众的ActionResult APPVIEW()
{
名单,LT;用途>应用;使用
(; ISiteDbContext> ISiteDbContext背景= _resolver.GetService<())
{
=应用context.Applications.ToList();
}
返回PartialView(APPVIEW,apps.OrderBy(一个= GT; a.Name).ToList());
}
渲染的部分 - 这是一个观点是在家庭控制器内<。 / p>
@ {Html.RenderPartial(〜/查看/应用/ AppView.cshtml,新Example.Services.DAL.Application( ));}
和我的应用程序查看
@model IEnumerable的< Example.Services.DAL.Application>
@ {
ViewBag.Title =应用程序;
}
< H2>应用< / H>
< P>
@ Html.ActionLink(添加应用程序,创建)
< / P>
<表>
< TR>
<第i个
@ Html.DisplayNameFor(型号=> model.Name)
< /第i
<第i< /第i
< / TR>
@foreach(以型号VAR项)
{
< TR>
< TD>
@ Html.DisplayFor(modelItem => item.Name)
< / TD>
< TD>
@ Html.ActionLink(编辑,编辑,新{ID = item.ID})|
@ Html.ActionLink(详细信息,详细信息,新{ID = item.ID})|
@ Html.ActionLink(删除,删除,新{ID = item.ID})
< / TD>
< / TR>
}
< /表>
完整的错误消息:
传递到字典中的模型产品类型
'Example.Services.DAL.Application',但这需要字典System.Collections中的类型
'一个
型项目.Generic.IEnumerable`1 [Example.Services.DAL.Application]'。
块引用>
解决方案由于错误指出要传递一个错误的类型。更改
@ {Html.RenderPartial(〜/查看/应用/ AppView.cshtml,新Example.Services.DAL。应用程序());}
到
@ {Html.RenderPartial(〜/查看/应用/ AppView.cshtml,新的List< Example.Services.DAL.Application> {新Example.Services.DAL.Application( )});}
I am getting this error and I'm not sure if I am able to do this, here is my code..
Application controller
public ActionResult AppView() { List<Application> apps; using (ISiteDbContext context = _resolver.GetService<ISiteDbContext>()) { apps = context.Applications.ToList(); } return PartialView("AppView", apps.OrderBy(a => a.Name).ToList()); }
Render partial - this is within a view which is in the home controller.
@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());}
and my application view
@model IEnumerable<Example.Services.DAL.Application> @{ ViewBag.Title = "Applications"; } <h2>Applications</h2> <p> @Html.ActionLink("Add New Application", "Create") </p> <table> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | @Html.ActionLink("Details", "Details", new { id = item.ID }) | @Html.ActionLink("Delete", "Delete", new { id = item.ID }) </td> </tr> } </table>
full error message:
The model item passed into the dictionary is of type 'Example.Services.DAL.Application', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Example.Services.DAL.Application]'.
解决方案As error states you are passing a wrong type. Change
@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());}
to:
@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new List<Example.Services.DAL.Application> { new Example.Services.DAL.Application() });}
这篇关于但本词典需要类型的“典范项目System.Collections.Generic.IEnumerable`1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!