MVC: Dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1
Solution 1
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() });}
Solution 2
Your AppView.cshtml
is bind to strongly type of @model IEnumerable<Example.Services.DAL.Application>
and while calling this view you are passing @{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());}
It should be the list object. You must pass list
of Example.Services.DAL.Application()
Change your
@{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() });}
Solution 3
your code is looking for a Ienumerable because what you pass to your partial view must be the same as what is in your view so try to change the first line of your application view to
@model Example.Services.DAL.Application
it worked for me hopefully it was of good use to you as well :D
Comments
-
Zi_31 over 3 years
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]'.