首页 > 解决方案 > 日期字段在 pagedlist 的第二页上失去价值

问题描述

我在分页中显示了来自数据库的表,并在两个日期(开始日期和完成日期)之间执行了搜索和过滤。问题是当我转到第二页时,日期重置并且表格没有过滤。

我检查了第二个页面何时称为 ViewBag.filterStartDate 并且 ViewBag.filterFinishDate 为空并且在控制器中 ViewBags 设置为 Now。

HomeController.cs:

using System;
using System.Linq;
using System.Web.Mvc;
using PagedList;

namespace DatabaseDisplayProject.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Get(string sortOrder, string searchValue, DateTime? filterStartDate,
                                DateTime? filterFinishDate, string currentFilter, int? page)
        {
            //If I don't Initialize ViewBag it would throw Exception Cannot perform runtime 
            //binding on a null reference
            ViewBag.filterStartDate = DateTime.Now;
            ViewBag.filterFinishDate = DateTime.Now;
            ViewBag.CurrentSort = sortOrder;
            ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "id_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            ViewBag.ExpensesSortParm = sortOrder == "Expenses" ? "expenses_desc" : "Expenses";

            if (searchValue != null)
            {
                page = 1;
            }
            else
            {
                searchValue = currentFilter;
            }

            ViewBag.CurrentFilter = searchValue;

            //Connection to Database
            AccountingEntities db = new AccountingEntities();

            //From each object in the collection
            var valuesOfTableExpenses = from s in db.Expenses
                                 select s;

            //Filters if a value is equal or is contined in Date, ID and Expenses
            if (!String.IsNullOrEmpty(searchValue))
            {
                valuesOfTableExpenses = valuesOfTableExpenses.Where(s => s.ID.ToString().Contains(searchValue)
                                                      || s.Date.ToString().Contains(searchValue)
                                                      || s.Expenses.ToString().Contains(searchValue));
            }

            
            //Here I check if filterStartDate has recieved a value from user 
            if (filterStartDate != null)
            {
                //I should use &&, if you use || if one condition is ok the row will be displaye and it won't go between
                valuesOfTableExpenses = valuesOfTableExpenses.Where(s => s.Date >= filterStartDate && s.Date <= filterFinishDate);
                ViewBag.filterStartDate = filterStartDate;
            }
            if (filterFinishDate != null)
            {
                ViewBag.filterFinishDate = filterFinishDate;
            }

            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(valuesOfTableExpenses.ToPagedList(pageNumber, pageSize));

获取.cshtml:

@model PagedList.IPagedList<DatabaseDisplayProject.Expens>
@using PagedList.Mvc;
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Get</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link href="~/Content/StyleSheet.css" rel="stylesheet" />

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

</head>
<body>
    @* This form searches inside the table *@
    @*Creates and HTML form. An HTML form is used to collect user input.*@
    @*FormMethod.Get makes the form uses HTTP GET*@
    @using (Html.BeginForm("Get", "Home", FormMethod.Get))
    {
        <p>
            
            Find by any value: @Html.TextBox("SearchValue", ViewBag.CurrentFilter as string)
            <input type="submit" value="Search" />
        </p>
    }


<form>
    <label for="Date Picker">Choose start date and end date</label>
    <input type="date" name="filterStartDate" id="filterStartDate" class="form-control date" value="@ViewBag.filterStartDate.ToString("yyyy-MM-dd")" />

   
    <input type="date" name="filterFinishDate" id="filterFinishDate" class="form-control date" value="@ViewBag.filterFinishDate.ToString("yyyy-MM-dd")" />

    <input type="submit">
</form>
    <hr />
    @using (Html.BeginForm("Index", "Home"))
    {
        <table cellpadding="0" cellspacing="10">
            <tr>
                <th>
                    <a href=@Url.Action("Get", "Home", new { sortOrder = ViewBag.IDSortParm,
                               currentFilter = ViewBag.CurrentFilter })>
                        <span class="glyphicon glyphicon-sort"> ID</span>
                    </a>
                </th>
                <th>
                    <a href=@Url.Action("Get", "Home" , new { sortOrder=ViewBag.DateSortParm,
                               currentFilter=ViewBag.CurrentFilter })>
                        <span class="glyphicon glyphicon-sort"> Date</span>
                    </a>
                </th>
                <th>
                    <a href=@Url.Action("Get", "Home" , new { sortOrder=ViewBag.ExpensesSortParm,
                               currentFilter=ViewBag.CurrentFilter })>
                        <span class="glyphicon glyphicon-sort"> Expenses</span>
                    </a>
                </th>

            </tr>
            @foreach (Expens expens in Model)
            {
                <tr>
                    <td>@expens.ID&nbsp;&nbsp;</td>
                    <td>@expens.Date&nbsp;&nbsp;</td>
                    <td>@expens.Expenses</td>
                </tr>
            }
        </table>
 }

    <br />
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

    @Html.PagedListPager(Model, page => Url.Action("Get",
        new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</body>
</html>

费用.cs:

    //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace DatabaseDisplayProject
{
    using System;
    using System.Collections.Generic;
    
    public partial class Expens
    {
        public int ID { get; set; }
        public int TitleID { get; set; }
        public System.DateTime Date { get; set; }
        public int Expenses { get; set; }
    }
}

标签: javascriptc#asp.netasp.net-mvcpaging

解决方案


推荐阅读