在PagedList上丢失当前过滤器

本文关键字:过滤器 PagedList | 更新日期: 2023-09-27 18:10:07

谁能帮帮我,告诉我哪里错了?我有一个显示错误列表的仪表板。顶部有一个带有type="date"的文本框,允许用户选择日期。然后,列表会自我更新,以显示在所选日期发生的错误。我想在这个上实现PagedList但是当我点击第二页时它没有显示任何结果

控制器

public ActionResult Index(string sortOrder, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
        var applications = from s in db.ElmahErrors
                           where s.TimeUtc == DateTime.Today
                           select s;
        switch (sortOrder)
        {
            default:
                applications = applications.OrderByDescending(x => x.TimeUtc);
                break;
        }
        int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
        int pageNumber = (page ?? 1);
        return View(applications.ToPagedList(pageNumber, pageSize));
    }
    [HttpPost]
    public ActionResult Index(string sortOrder, string currentFilter, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

        DateTime userSelectedDate = DateTime.Parse(Request["datePicker"]).Date;
        string dateString = userSelectedDate.ToString("yyyy-MM-dd");
        if (dateString != null)
        {
            page = 1;
        }
        else
        {
           dateString = currentFilter;
        }
        ViewBag.CurrentFilter = dateString;
        var startDate = userSelectedDate.Date;
        var endDate = startDate.AddDays(1);
        var applications = from s in db.ElmahErrors
                           //where s.TimeUtc >= startDate && s.TimeUtc < endDate
                           select s;
        if (!String.IsNullOrEmpty(dateString))
        {
            applications = applications.Where(s => s.TimeUtc >= startDate && s.TimeUtc < endDate);
        }
        switch (sortOrder)
        {
            default:
                applications = applications.OrderByDescending(x => x.TimeUtc);
                break;
        }
        int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
        int pageNumber = (page ?? 1);
        return View(applications.ToPagedList(pageNumber, pageSize));
    }
<<h3>视图/h3>
@model PagedList.IPagedList<DataIntelligence.Models.ElmahError>
@using PagedList.Mvc;
<link href="Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
    ViewBag.Title = "Errors By Day";
}
<div class="jumbotron">
    <h2>Errors By Day</h2>
</div>
@using (Html.BeginForm("Index", "Day", FormMethod.Post, new { id = "frmSearch" }))
{
    <label for="datePicker">Find By Date:</label>
    @Html.TextBox("datePicker", ViewBag.CurrentFilter as string, new { @type = "date"})
}
<script>
    $("#datePicker").change(function () {
        $("#frmSearch").submit();
    });
</script>
<table>
    <tr>
        <th>
            Application
        </th>
        <th>
            Host
        </th>
        <th>
            Type
        </th>
        <th>
            Date
        </th>
        <th>
            Message
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td style="width:400px;">
                <a href="@Url.Action("Details", new { id=item.ErrorId})"> @Html.DisplayFor(modelItem => item.Application) </a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Host)
            </td>
            <td style="width:550px;">
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TimeUtc)
            </td>
            <td>
                @{
                    string parameterValue = item.Message.Substring(0, 8);
                }
                @Html.DisplayFor(modelItem => parameterValue)
            </td>
        </tr>
    }
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

我试过从POSTGET,但我似乎不能得到我的头周围如何让它工作。

有人能告诉我要改变什么才能让我的PagedList工作吗?

在PagedList上丢失当前过滤器

PagedList,正如您可能发现的大多数分页模块一样,在单击下一页时使用GET,而不是POST。因此,请求不会到达标记为HttpPost的操作,而是到达默认操作。因此,没有应用日期筛选器,您将收到带有今天错误的第二页。我猜今天没有足够的错误(很好,嗯?)来填充第二页的数据。

我建议您切换到只处理GET请求的单个操作。毕竟,这是数据查看等幂等操作的推荐方法。理想情况下,只有在服务器上更改数据时才应该使用POST。