【问题标题】:The model item passed into the dictionary requires a model item of type 'PagedList.IPagedList传入字典的模型项需要类型为“PagedList.IPagedList”的模型项
【发布时间】:2018-07-05 14:16:50
【问题描述】:

[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 中的项目列表传递给查看页面。谢谢

【问题讨论】:

    标签: c# model-view-controller


    【解决方案1】:

    您将返回查看jo(此处为View(jo)

    return View(jo);
    

    不属于IPagedList&lt;OpenOrderFramework.Models.Item&gt;

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

    旁注:我不确定您在创建 jo 时通过在 items 上执行一些 linq 所做的操作,但后来您又在 items 上执行了一些操作,并且而是返回jo。所以你需要删除那里多余的代码。

    【讨论】:

      【解决方案2】:

      您正在返回jo,但您不想返回items吗?

       return View(items);
      

      或将jo 设为 PagedList...

      【讨论】:

      • 我想返回 var(jo) 中的项目列表
      • 那你得把jo变成PagedList
      • 你能帮帮我吗
      • return View(jo.ToList().ToPagedList(page ?? 1, 10));
      • 它抱怨这个:附加信息:方法'First'只能用作最终查询操作。请考虑在此实例中使用“FirstOrDefault”方法。
      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 2013-10-17
      • 2015-09-27
      • 2014-02-02
      • 1970-01-01
      相关资源
      最近更新 更多