How do I maintain a selection in my MVC 5 view's dropdownlist while paging?

懵懂的女人 提交于 2021-02-19 07:20:24

问题


I have an MVC 5 view with a search filter textbox, a search filter date dropdownlist, several sortable columns and using PgedList. Everything is working perfectly except I would like the DropDownList to maintain it's selection as the results are navigated from page to page. The filtering by the date Dropdown works just fine and is retained from page to page but I haven't figured out how to stop the dropdown list from reverting to the top selection "All" on subsequent pages (even though the date selection filter is maintained properly while paging).

Here is the controller code:

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

Here is the view:

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

来源:https://stackoverflow.com/questions/21289519/how-do-i-maintain-a-selection-in-my-mvc-5-views-dropdownlist-while-paging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!