传递到字典中的模型项需要类型为'PagedList.IPagedList的模型项

时间:2018-07-05 14:16:51

标签: c# model-view-controller

[1]我遇到此错误传递到字典中的模型项的类型为'System.Collections.Generic.List,但此字典     需要类型为'PagedList.IPagedList`1 [OpenOrderFrame

i have my controller as this : 

public class ItemsController : Controller
        {
            private ApplicationDbContext db = new ApplicationDbContext();    
        // GET: Items
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {                      
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "Name";
            ViewBag.PriceSortParm = sortOrder == "Name" ? "Loc_desc" : "Location";
            IEnumerable<Item> items = db.Items;
            var jo = (from a in items.GroupBy(x => new { x.Catagorie, x.ItemName })
                                     .Select(g => new
                                     {
                                         Category = g.Key.Catagorie,
                                         ItemName = g.Key.ItemName,
                                         Quantity = g.Sum(s => s.ItemQty),
                                         Location = g.First().Location
                                     })
                                    select a);
if (!string.IsNullOrWhiteSpace(searchString))
            {
                items = items.Where(s => s.ItemName.ToUpper().Contains(searchString.ToUpper())
                                           || s.Catagorie.Name.ToUpper().Contains(searchString.ToUpper())||
                                           s.Location.Name.ToUpper().Contains(searchString.ToUpper())).ToList().ToPagedList(page ??1,  20);
                //}

            }
            else
            {
                items = items.ToList().ToPagedList(page ?? 1, 10);
            }
            return View(jo);

在我的视图中,我的代码如下:

@model PagedList.IPagedList<OpenOrderFramework.Models.Item>
@using PagedList.Mvc;
@using PagedList;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Food";
}

[2]请帮助我提供代码,我真受够了 我想通过jo中的项目列表来查看页面。谢谢

2 个答案:

答案 0 :(得分:1)

您将在此处返回视图joView(jo)

return View(jo);

其类型不是IPagedList<OpenOrderFramework.Models.Item>

您似乎错误地将jo变量而不是items传递回了View方法。将其更改为通过items即可解决。

侧面说明:我不确定您在创建jo时通过在items上执行一些linq来做什么,但是后来您又在items,然后返回jo。因此,您需要删除其中多余的代码。

答案 1 :(得分:0)

您要返回jo,但是您不想返回items吗?

 return View(items);

或者将jo设为PagedList ...