Paging asp mvc



  • How is the asp mvc literate? Cases are as follows. There's an object that's on the page.

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

    I'm getting a list of these objects as a result of a search for OBD. I'll use it. 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 it's kind of working, but only until the pages change, Ist's getting it all. What am I doing wrong? What are the options for keeping lst? Shouldn't we be searching for the OBD every time and returning the list?



  • lst does not survive crossings between pages - MVC creates a new copy of the counteraller class for each request. Moreover, with the actual deployment of applications, several servers are usually used, and a second-page request can reach another physical server.

    You should re-entry the database every time. Also desirable ToPagedList the original IQueryableand not from the entire list of objects in memory, only a specific page will be downloaded from the base.

    There are options for keeping lst.

    • In the cash, available through HttpContext.Cachecommon to all users + should decide in advance how to disabled it when changing the list.
    • In session https://ru.stackoverflow.com/a/424111/177221 )

    But in a small annex, it's still easier to retrieve the data from the base.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2