MVC: Dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1

蹲街弑〆低调 提交于 2019-11-29 05:03:54

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() });}

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() });}

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

In order to use sorting on a collection of (custom) Objects, you need a method for sorting it. Usually, this is accomplished by inheriting the "IComparable" interface. In your Object class, you then need a method "Compare" to determine the method of comparing the instances of your Objects for sorting (I use "Date" in my project).

To recap:

You use this in your Application Controller:

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

But in order to actually sort (or in this case OrderBy), you need a method in your "Application" class that compares the instances in the list to sort them. This is done using the "Compare" method:

int Compare(Object x, Object y)

How you compare is totally up to you. The result however, is either:

  • Less than zero: Object x < Object y
  • Zero: Object x = Object y
  • Greater than zero: Object x > Object y

I hope this helps. Good luck!

Dear regards, Björn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!