MVC: a dictionary requires a model element of type "System.Collections.Generic.IEnumerable`1

I get this error and I'm not sure if I can 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());
}

Partially rendering is an element that resides in the home controller.

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());} 

and presentation of my 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>

full error message:

The model element passed to the dictionary is of type 'Example.Services.DAL.Application', but this model requires the model type 'System.Collections.Generic.IEnumerable`1 [Example.Services.DAL.Application]'.

+4
source share
4 answers

The error indicates that you are passing the wrong type. Change

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());}

at

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new List<Example.Services.DAL.Application> { new Example.Services.DAL.Application() });}
+3

AppView.cshtml @model IEnumerable<Example.Services.DAL.Application>, @{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());}

. list Example.Services.DAL.Application()

@{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() });}
+2

Ienumerable, , , , ,

@model Example.Services.DAL.Application

, , : D

+1

() , . "IComparable". Object "", ( "" ).

:

Application Controller:

PartialView ( "AppView", apps.OrderBy(a = > a.Name).ToList());

( OrderBy) "", . "":

int (Object x, Object y)

, . , , :

  • : x < y
  • Zero: x = y
  • : x > y

, . !

,

0

All Articles