MVC:字典需要类型为“System.Collections.Generic.IEnumerable"1 的模型项 [英] MVC: Dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1
问题描述
我收到这个错误,我不确定我是否能够做到这一点,这是我的代码..
I am getting this error and I'm not sure if I am able to do this, here is my code..
应用控制器
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());}
和我的应用程序视图
@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>
完整的错误信息:
传入字典的模型项是类型'Example.Services.DAL.Application',但是这本字典需要一个型号型号'System.Collections.Generic.IEnumerable`1[Example.Services.DAL.Application]'.
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());}
到:
@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new List<Example.Services.DAL.Application> { new Example.Services.DAL.Application() });}
这篇关于MVC:字典需要类型为“System.Collections.Generic.IEnumerable"1 的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!