PagedList - a way to use not with the whole collection

902 Views Asked by At

I've got simple action in which I make a request to web service to get a LIST<> of articles for specific group.

Then using pagedList (and specifying the desired page and page number) of course I specify the subset of that list I want to take. The problem is : the article for specific group can be thousand for example - and getting the info for all of them from the web service takes a lot of time and even sometimes crushes (when the articles are above 1000)

Is there a way to get the articles only for the specific page and still to use pagedList because I see that unfortunately we have to call ToPagedList method for the whole collection.

public virtual ActionResult ImportShow(String id, int? menuID, string articlegroupID, string menuforHistory,int? counter,int?page,int? pageSize,string articleDescr, int?  ArticleID)
        {



                List<WebServiceBeaMenu> standartList = ebServiceBea.GetArticle(Convert.ToInt32(menuID), articlegroupID, "", articleDescr);
                IPagedList<WebServiceBeaMenu> p_ProductsShow = standartList.ToPagedList(actualpage,actualPageSize);

                p_GroupMenu.ProductMenu = p_ProductsShow;
                p_GroupMenu.MenuHistory = p_GetMenuHistory.ToList();
                p_GroupMenu.MenuLeft = p_GetMenuLeft.ToList();
                return PartialView("ImportShow", p_GroupMenu);                 
            }
        }

here is my view

@model  MvcBeaWeb.GroupMenu



        @for (int i = 0; i < Model.ProductMenu.Count; i++)
        {

        <div>
            var item = Model.ProductMenu[i];



            @Html.PagedListPager(Model.ProductMenu, page => Url.Action("ImportShow", new { id = Model.LanguageName, menuID = @Session["men"], articlegroupID = Session["article"], articleDescr = Session["articleDescr"], pageSize = Session["pageSize"], page }))

    </div>
1

There are 1 best solutions below

5
On BEST ANSWER

You should rewrite .GetArticle() Or replace it with whith somethink like .GetPagedArticle() if you have access to WebService. This methid should have all paging params. That's the only way i think. Your .GetArticle() method should return object like this:

public class Set<T>
{
    public Set()
    {
        Elements = new List<T>();
    }

    public Set(List<T> elements, int rowsAll)
    {
        Elements = elements;
        RowsAll = rowsAll;
    }

    public Set(List<T> elements, int rowsOnPage, int pageSelected, int rowsAll)
    {
        Elements = elements;
        PageSelected = pageSelected;
        RowsOnPage = rowsOnPage;
        RowsAll = rowsAll;
        PagesAll = (rowsAll % RowsOnPage == 0) ? rowsAll / RowsOnPage : rowsAll / RowsOnPage + 1; ;
    }
    public int RowsOnPage { get; set; }
    public List<T> Elements { get; set; }
    public int? RowsAll { get; set; }
    public int PageSelected { get; set; }
    public int PagesAll { get; set; }
}

Where Elements should be not all elements, but only paged one.