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屋!