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?