【问题标题】:Losing Current Filter on PagedList在 PagedList 上丢失当前过滤器
【发布时间】:2015-11-21 16:16:18
【问题描述】:

有人可以帮助我并告诉我哪里出错了。我有一个显示错误列表的仪表板。顶部有一个带有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));
    }

查看

@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 }))

我尝试从 POST 更改为 GET,但我似乎无法弄清楚如何让它工作。

有人可以建议我改变什么以使我的 PagedList 正常工作吗?

提前致谢。

【问题讨论】:

    标签: c# asp.net asp.net-mvc pagedlist


    【解决方案1】:

    PagedList,正如您可能发现的most of paging modules,在您单击下一页时使用 GET,而不是 POST。所以请求到达的不是标有HttpPost 的操作,而是默认的操作。因此不应用日期过滤器,您会收到包含今天错误的第二页。我猜今天没有足够的错误(很好,嗯?)来填充第二页的数据。

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

    【讨论】:

    • 试过了,但它停止了Request["datePicker"]的工作,这给了我错误String reference not set to an instanceof String
    • @D.M,请注意,您还需要表单才能使用 FormMethod.Get。而datePicker确实应该是action方法的输入参数
    • 是的,它也改变了,我对此很陌生,如果这应该很容易,我很抱歉
    猜你喜欢
    • 2013-09-14
    • 2018-12-30
    • 2011-06-10
    • 2012-07-05
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多