【问题标题】:The model item passed into the dictionary is of type 'List`, but this dictionary requires a model item of type 'PagedList.IPagedList`1 [duplicate]传入字典的模型项是“List”类型的,但该字典需要“PagedList.IPagedList”1 类型的模型项 [重复]
【发布时间】:2018-06-18 13:05:56
【问题描述】:

当我运行我的视图时,我得到了这个错误,我知道错误是关于什么的,我知道之前问过这个问题,所以请不要投票给我,让我解释一下 :) 我尝试创建新的viewModel,然后将我的viewModel包装到PagedList.IPagedList中,但是当我这样做时,我无法访问我的属性,然后我调试并发现我应该将我的ViewModel(RMAHistory)的类型转换为PagedList.IPagedList或一些我的 ViewModel 如何接受 PagedList.IPagedList ,但老实说我不知道​​如何:) :)

谁能指出我正确的方向:)
在此先感谢:)

错误: 传入字典的模型项的类型为 System.Collections.Generic.List1[ModelNameSpace.Models.RMAHistory]', but this dictionary requires a model item of type 'PagedList.IPagedList1[ModelNameSpace.Models.RMAHistory]。

控制器:

public ActionResult RMAList(string searchingrma, int? pageNumber) 
    {

        List<RMAHistory> query = db.RMAStatus.Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y }).
        Where(x => x.y.Ordrenummer.Contains(searchingrma) && x.y.Fakturnummer.Contains(searchingrma) || searchingrma == null).
        Select(t => new RMAHistory
        {

            OrdreDato = t.y.OrdreDato,
            AntalRMA = t.y.AntalRMA


        }).OrderBy(t => t.OrdreDato).ToPagedList(pageNumber ?? 1, 5).ToList();


        return View(query);




    }


查看:

@model IPagedList<ModelNameSpace.Models.RMAHistory>
@using PagedList;
@using PagedList.Mvc;

<table>

<tbody>

foreach (var rma in Model)
{
<tr>

<td class="tdft">@rma.OrdreDato.ToString("dd/MM/yyy")</td>
<td class="tdft">@rma.AntalRMA</td>

 }

</tr>
}

</tbody>


</table>

 @Html.PagedListPager(Model, pageNumber => Url.Action("RMAList", new
                        {
                            pageNumber,

                            searching = Request.QueryString["searchingrma"]

                        }))


视图模型:

public class RMAHistory
{
    public int? Id { get; set; }
    public DateTime OrdreDato { get; set; }
    public string AntalRMA { get; set; }

}

【问题讨论】:

  • List&lt;RMAHistory&gt; query 更改为 IPagedList&lt;RMAHistory&gt; 并从查询中删除 .ToList()
  • @StephenMuecke 谢谢谢谢谢谢,你太棒了:)
  • Tanmay 已经添加了 :)
  • stackoverflow.com/questions/19254055/… 可能是更好的副本。

标签: c# asp.net-mvc pagedlist


【解决方案1】:

错误消息是不言自明的。您的操作方法应该返回IPagedList&lt;ModelNameSpace.Models.RMAHistory&gt;,而您正在返回List&lt;RMAHistory&gt;

所以,你的代码是:

 public ActionResult RMAList(string searchingrma, int? pageNumber) 
        {

            var query = db.RMAStatus.Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y }).
            Where(x => x.y.Ordrenummer.Contains(searchingrma) && x.y.Fakturnummer.Contains(searchingrma) || searchingrma == null).
            Select(t => new RMAHistory
            {

                OrdreDato = t.y.OrdreDato,
                AntalRMA = t.y.AntalRMA


            }).OrderBy(t => t.OrdreDato).ToPagedList(pageNumber ?? 1, 5);


            return View(query);
        }

【讨论】:

  • 是的,我知道,我只是不知道如何将我的视图模型上的类型转换为 IPagedList,直到@StephenMuecke 帮助我:) 无论如何感谢您的回答,它也是正确的:)
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多