简体   繁体   English

分页浏览数据集而无需在MVC C#中获取新数据集

[英]Page through dataset without getting new dataset in MVC c#

I have 3 different functions on my page. 我的页面上有3种不同的功能。 1.) Search, 2.) Sort and 3.) Paging 1.)搜索,2。)排序和3.)分页

I have all of this working, but I was wondering if anyone know how, or can point me somewhere that shows how to save, per user, the dataset so they can sort and page without searching again and then also when the user Searches to pull the data again. 我已经完成了所有这些工作,但是我想知道是否有人知道如何或者可以向我指出可以显示每个用户如何保存数据集的位置,以便他们可以进行排序和分页而无需再次搜索,也可以在用户搜索拉动时数据。

I'm currently using MVC5, Bootstrap, and PagedList.MVC 我目前正在使用MVC5,Bootstrap和PagedList.MVC

Here's an example of what I need. 这是我需要的一个例子。 I honestly am not sure how to or even if I should cache the data, but it seems like I would want to as this data changes regularly (they are events from a DB) and I'd like the user to work with the dataset they searched and not with a new dataset every time they page or sort. 老实说,我不确定如何缓存数据,或者即使我应该缓存数据,但由于该数据会定期更改(它们是来自数据库的事件),因此我想让我使用该数据集,每次分页或排序时都进行搜索,而不使用新的数据集。

private List<Models.EventItem> events = null;

[HttpPost]
public ActionResult ViewLogs(ViewModels.ViewLogs model)
{
    if (model.ActionPerformed == "search")
    {
        // it was a search, so let's pull new events in.
        events = Business.Events.GetEvents(GetCurrentSearchParameters(model));
    }

    if (model.ActionPerformed == "sort")
    {
        events = null // need to grab events from the cached dataset?
        // then need to perform the sort on the cached dataset.
    } 

    if (model.ActionPerformed == "page")
    {
        events = null // need to grab events from the cached dataset?
    }

    model.EventItems = events.ToPagedList(model.CurrentPage, model.PageSize);
    return View("ViewLogs", model);
}

[HttpGet]
public ActionResult ViewLogs()
{
    // ViewLogs (Get) will take defaults instead.
    ViewModels.ViewLogs model = new ViewModels.ViewLogs();

    events = Business.Events.GetEvents(GetCurrentSearchParameters(model));
    model.EventItems = events.ToPagedList(model.CurrentPage, model.PageSize);

    return View(model);
}

Based on the comments under my question, you can go about this 2 ways. 根据我的问题下的评论,您可以采用以下两种方法。 The not-so-great way, but works is using Session to save the data and conditionally populate it. 不太好用,但是可行的是使用Session保存数据并有条件地填充它。

What I've done research on is to actually use SQL to do the paging. 我所做的研究是实际使用SQL进行分页。 In SQL you set something up similar to this which will give you total row count, total page count, and then whatever page worth of data. 在SQL中,您进行了与此类似的设置,这将为您提供总行数,总页数,以及随后的任何页面数据。 From what I understand, especially dealing with large chunks of data, this can save a massive amount of time. 据我了解,尤其是处理大量数据,这可以节省大量时间。 Thanks to all for putting me on the right track. 感谢所有人使我走上正轨。

BEGIN
DECLARE @fromDate DATETIME = DATEADD(d, -30, GETUTCDATE()) 
DECLARE @page INT = 1
DECLARE @perPage INT = 25
DECLARE @totalRows INT = 0
DECLARE @totalPages INT = 0
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   --
   SELECT @totalRows = COUNT(*)
   FROM 
          dbo.[Events] a
   WHERE
          a.[EventTime] >= @fromDate
   ;
   --
   BEGIN TRY
          SET @totalPages = CEILING(@totalRows / @perPage); 
   END TRY
   BEGIN CATCH
          SET @totalPages = 0
   END CATCH
   --
   SELECT @totalRows AS [TotalRows], @totalPages AS [TotalPages];
   --
   --
   SELECT
          a.[EventTime],
          a.[Column1],
          a.[Column2]
   FROM 
          dbo.[Events] a
   WHERE
          a.[EventTime] >= @fromDate
   ORDER BY 
          a.[EventTime] DESC
          OFFSET (@page * @perPage) ROWS
          FETCH NEXT @perPage ROWS ONLY
   ;
END

There's a pretty in-depth article on it here: http://sqlperformance.com/2015/01/t-sql-queries/pagination-with-offset-fetch 此处有一篇非常深入的文章: http : //sqlperformance.com/2015/01/t-sql-queries/pagination-with-offset-fetch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM