How pagination is properly implemented in asp mvc? Cases as follows. There is a certain object that is displayed on the page.

public class Main { string Akt_Name { get; set; } [Key] public string Reg_Num { get; set; } } 

I get a list of these objects as a result of searching in the database. Well, then I use PagedList.MVC

 List<Main> lst = new List<Main>(); - объявляю как глобальный. return PartialView(lst.ToPagedList(pageNumber, pageSize)); 

Presentation Code:

 @using PagedList.Mvc; @model PagedList.IPagedList<LocalEtalon_mvc.Models.Main> @using WebApp.Models; <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /> @foreach (Main item in Model) { <li> <span class="S_regnum">@item.Reg_Num</span> <span class="S_aktname">@item.Akt_Name</span> <br /> <a id="aRegnum_@item.Reg_Num" class="Aktcard" href="#">карта</a> <a id="tRegnum_@item.Reg_Num" class="Akttext" href="#">текст</a> </li> } Страница @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) из @Model.PageCount @Html.PagedListPager(Model, page => Url.Action("changePage", new { page })) <script src="~/Scripts/main.js"></script> 

And everything seems to work, but only until the page changes. lst vanishes and that's it. What am I doing wrong? What are the lst storage options? Do not have every time to search the database and return the list of the found?

    1 answer 1

    lst does not survive page transitions — MVC creates a new instance of the controller class for each request. Moreover, during the actual deployment of applications, several servers are usually used, and the request for the second page can go to another physical server.

    You should re-select data from the database every time. It is also desirable to call ToPagedList on the original IQueryable , and not on the entire list of objects selected in memory - then only a specific page will be loaded from the database.

    There are options with lst storage

    But in a small application, it is still easier to re-select data from the database.

    • Вам стоит заново выбирать данные из базы каждый раз. Желательно также при этом вызывать ToPagedList у оригинального IQueryable, а не у всего списка объектов, выбранного в память - тогда из базы будет загружена только конкретная страница. But could not a little more? Or link. Doing the web is not long, little experience. But the application should work with a large amount of information (plus the complexity of queries to the database). - Rans
    • one
      @Rans most likely you are now making a request through something like EF, then call ToList() and add the result to lst. Thus, you select all objects from the database. Instead, remove the ToList() call, and call ToPagedList() directly at your database request. More hard to say without seeing your code :( - PashaPash
    • @Rans read about LINQ and about at what point when working through it a query to the database is executed. - PashaPash
    • You are completely right, if in general, then this is exactly what happens to me. I will read about LINQ again. I still have one more question. In fact, I have a search form and search results that I’m trying to display. Those. when submitting, a method searches the database and forms a list. And what about in this case when you click on the page number? Where to collect information on the search criteria? There is a form on the page (I made the page through BS Tab), but before that they couldn’t get there ( - Rans
    • @Rans add search criteria to the model, and on the view make them part of the link - pass the parameters to Url.Action("changePage", new { page } . - PashaPash