2

我有一个带有搜索过滤器文本框、搜索过滤器日期下拉列表、几个可排序列并使用 PgedList 的 MVC 5 视图。一切都运行良好,除了我希望 DropDownList 在结果从页面导航到页面时保持它的选择。按日期过滤的下拉菜单工作得很好,并且在页面之间保留,但我还没有弄清楚如何阻止下拉列表恢复到后续页面上的顶部选择“全部”(即使日期选择过滤器被维护)分页时正确)。

这是控制器代码:

    public ViewResult Index( DateTime? datefilter, string sortOrder, string searchString, string currentFilter, int? page)
    {
        var DateLst = new List<DateTime>();

        var DateQry = from d in db.vATrRpt5
                       orderby d.saleDate
                       select d.saleDate;

        DateLst.AddRange(DateQry.Distinct());
        ViewBag.DateFilter = new SelectList(DateLst);

        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
        ViewBag.ProductSortParm = sortOrder == "Prod" ? "Prod_desc" : "Prod";
        ViewBag.DateSortParm = sortOrder == "Date" ? "Date_desc" : "Date";
        ViewBag.StateSortParm = sortOrder == "State" ? "State_desc" : "State";

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

        if (datefilter != null)
        {
            ViewBag.DateSelection = datefilter;
        }

        ViewBag.CurrentFilter = searchString;


        var tracings = from t in db.vATrRpt5
                       select t;
        if (!String.IsNullOrEmpty(searchString))
        {
            tracings = tracings.Where(t => t.Vendor.ToUpper().Contains(searchString.ToUpper())
                                    || t.ProductID.ToUpper().Contains(searchString.ToUpper())
                                    || t.LastName.ToUpper().Contains(searchString.ToUpper()));

        }

        if (datefilter.ToString() != "")
        {
            tracings = tracings.Where(z => z.saleDate == datefilter);
        }

        switch (sortOrder)
        {
            case "Name_desc":
                tracings = tracings.OrderByDescending(t => t.Vendor);
                break;
            case "Prod":
                tracings = tracings.OrderBy(t => t.ProductID);
                break;
            case "Prod_desc":
                tracings = tracings.OrderByDescending(t => t.ProductID);
                break;
            case "Date":
                tracings = tracings.OrderBy(t => t.saleDate);
                break;
            case "Date_desc":
                tracings = tracings.OrderByDescending(t => t.saleDate);
                break;
            case "State":
                tracings = tracings.OrderBy(t => t.State);
                break;
            case "State_desc":
                tracings = tracings.OrderByDescending(t => t.State);
                break;
            default:
                tracings = tracings.OrderBy(t => t.Vendor);
                break;
        }

        int pageSize = 25;
        int pageNumber = (page ?? 1);

        return View(tracings.ToPagedList(pageNumber, pageSize));
    }

这是视图:

@model PagedList.IPagedList<TWAArchiveViewer.Models.vATrRpt5>
       @using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Tracings Archive Viewer";
}

<h2>Tracings Archive</h2>

@using (Html.BeginForm("Index", "Archive", FormMethod.Get))
{
<p>
    Find Product, Last Name, or Vendor: @Html.TextBox("SearchString",      ViewBag.CurrentFilter as string)
    Date: @Html.DropDownList("dateFilter", "All")
    <input type="submit" value="Search" />

    </p>
}


<table>
    <tr>
        <th>
            @Html.ActionLink("Vendor", "Index", new { sortOrder = ViewBag.NameSortParm,    currentFilter = ViewBag.CurrentFilter, dateFilter = ViewBag.DateSelection    })..................
        </th>
        <th>
            Last Name...............
        </th>
        <th>
            @Html.ActionLink("Product ID", "Index", new { sortOrder = ViewBag.ProductSortParm, currentFilter = ViewBag.CurrentFilter, dateFilter = ViewBag.DateSelection }).........
        </th>
        <th>
            Qty..........
    </th>
    <th>
        @Html.ActionLink("Sale Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter, dateFilter = ViewBag.DateSelection })....
        @*Sale Date....*@
    </th>
    <th>
        @Html.ActionLink("State", "Index", new { sortOrder = ViewBag.StateSortParm, currentFilter = ViewBag.CurrentFilter, dateFilter = ViewBag.DateSelection })
        @*State*@
    </th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Vendor)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.saleDate, "ShortDateTime")
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.State)
        </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,   dateFilter = ViewBag.DateSelection }))
4

0 回答 0