PagedList用的ViewModels和.Skip()的工作。以() [英] PagedList working with ViewModels and .Skip().Take()

查看:184
本文介绍了PagedList用的ViewModels和.Skip()的工作。以()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的控制器的动作让 MyObjectViewModels 的列表。

I have an Action in my Controller to get a list of MyObjectViewModels.

里面的那个动作的方法,我调用一个方法(服务层),通过采用一堆 LINQ 方法获取相关模型和返回 IQueryable的< MyObject的方式>

Inside that Action method, I call a method (Service layer) that fetches the relevant Models by applying a bunch of LINQ methods and returns a IQueryable<MyObject>.

然后我做了一些对返回的IQueryable 排序事后,理想情况下,我将执行 .Skip(...)。以( 25)中所产生的的IQueryable ,将其转换为一个列表,遍历25 为MyObject 元素,并将它们转换成我的 MyObjectViewModels 类。那么我会返回一个 PagedList 与的ViewModels:

Then I do some sorting on the returned IQueryable and afterwards, ideally, I would perform a .Skip(...).Take(25) in the resulting IQueryable, convert it to a List, iterate over the 25 MyObject elements and convert them into my MyObjectViewModels class. I'd then return a PagedList with those ViewModels:

public ActionResult MyAction(...parameters...)
{
    // Supose this returns 100000 records...
    IQueryable<MyObject> myObjs = _myService.GetMyObjects(...some params...);     

    // Sort and take only 25 (apply paging)
    IQueryable<MyObject> mySortedAndPagedObjs = myObjs.OrderBy(...);
    mySortedAndPagedObjs = mySortedAndPagedObjs.Skip(...).Take(25);

    List<MyObject> myObjsList = mySortedAndPagedObjs.ToList();
    List<MyObjectViewModel> myVMList = new List<MyObjectViewModel>();

    // *** Conversion loop ***
    foreach(MyObject myObj in myObjectList)
    {
       // ... convert the 25 MyObject to MyObjectViewModel 
       // and add them to the myVMList...
    }

    // Return a View with the PagedList
    return View(myVMList.ToPagedList(pageNumber, pageSize));
    // --> The PagedList will not "know" that it is looking at 25 records of a 100000 long list!
}

这似乎与 PagedList.MVC 的方法,它预计,<$不相容C $ C> ToPagedList()方法来接收的全部的组对象(不只是25 - 我认为它在内部调用 .Skip( )。取()调用自身)。这将,但是,逼我在申请整套MyObject来的是转换回路(!!)我宁愿只是做了25个项目,我想去的时候返回...

This, seems incompatible with the PagedList.MVC approach, which expects the ToPagedList() method to receive the entire set of objects (not just the 25 - I assume it internally calls the .Skip().Take() call itself). That would, however, force me to apply that conversion loop on the entire set of MyObject (!!) when I'd rather just do it for the 25 items I want to return at a time...

如何能得到我的 PagedList 显示的ViewModels 从一个子集选择(如25条记录)有很多数据表的?

How can I get my PagedList to show the ViewModels chosen from a subset (e.g. 25 records) of a table with lots of data?

推荐答案

这竟是 RTFM <案例/ A>,我想出了解决方案,我重新阅读文档时,我与我的问题完了。我会后的问答集。反正,因为它可能是有用的人,将来

This was actually a case of RTFM and I came up with the solution as I re-read the documentation when I was finished with my question. I'll post the Q&A anyway as it might be useful to someone in the future.

为了完成上述情况下,我不得不创建并返回一个 StaticPagedList

In order to accomplish the above scenario, I had to create and return a StaticPagedList.

在某些情况下,你没有能力创造一个访问的东西
  IQueryable的,例如使用。NET的内置的MembershipProvider的作为时
  GetAllUsers方法。此方法提供了寻呼,但不能经由IQueryable的。
  幸运的是PagedList仍然有你的背部(注意使用
  StaticPagedList):

In some cases you do not have access something capable of creating an IQueryable, such as when using .Net's built-in MembershipProvider's GetAllUsers method. This method offers paging, but not via IQueryable. Luckily PagedList still has your back (note the use of StaticPagedList):

在我而言,这意味着:

public ActionResult MyAction(...parameters...)
{

    // ... same as above ...

    // Create a StaticPagedList
    StaticPagedList<MyObjectViewModel> staticPagedList = new StaticPagedList<MyObjectViewModel>(myVMList , pageNumber + 1, pageSize, myObjs.Count());

    // Return a View with the StaticPagedList
    return View(myStaticPagedList);
}

这篇关于PagedList用的ViewModels和.Skip()的工作。以()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆