Пейджинг прерван при фильтрации MVC - PullRequest
0 голосов
/ 07 марта 2019

Я пытаюсь заставить пейджинг работать с несколькими поисками и порядками сортировки.Когда я выполняю поиск, первая возвращаемая страница является правильной страницей набора результатов, но когда я использую кнопки нумерации страниц для выбора следующей страницы, мой поиск больше не учитывается, и я получаю все результаты.

Мой контроллер выглядит так:

public ActionResult Index(string sortOrder, string searchString, string currentFilter, int? page, string Site, string Name, string Acct, string City, string State, string Address, string Status, string Type)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewBag.AddressSortParm = sortOrder == "Address" ? "addr_desc" : "Address";
        ViewBag.SiteSortParm = sortOrder == "Site" ? "site_desc" : "Site";
        ViewBag.CitySortParm = sortOrder == "City" ? "city_desc" : "City";
        ViewBag.StateSortParm = sortOrder == "State" ? "state_desc" : "State";

        if (searchString != null || Site != null || Name != null || Acct != null || City != null || State != null || Address != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var geocodes = from s in db.CFN_Geocodes select s;

        if (!String.IsNullOrEmpty(Name))
        {
            geocodes = geocodes.Where(s => s.NAME.Contains(Name));
        }

        if (!String.IsNullOrEmpty(Site))
        {
            geocodes = geocodes.Where(s => s.CFN_SITE.Contains(Site));
        }

        if (!String.IsNullOrEmpty(Address))
        {
            geocodes = geocodes.Where(s => s.STREET1.Contains(Address));
        }

        if (!String.IsNullOrEmpty(City))
        {
            geocodes = geocodes.Where(s => s.CITY.Contains(City));
        }

        if (!String.IsNullOrEmpty(State))
        {
            geocodes = geocodes.Where(s => s.STATE_CODE.Contains(State));
        }

        if (!String.IsNullOrEmpty(Acct))
        {
            geocodes = geocodes.Where(s => s.AccountNumber.Contains(Acct));
        }

        if (!String.IsNullOrEmpty(Status))
        {
            geocodes = geocodes.Where(s => s.Status.ToString().Contains(Status));
        }

        if (!String.IsNullOrEmpty(Type))
        {
            geocodes = geocodes.Where(s => s.SiteType.Contains(Type));
        }

        switch (sortOrder)
        {
            case "name_desc":
                geocodes = geocodes.OrderByDescending(s => s.NAME);
                break;
            case "Address":
                geocodes = geocodes.OrderBy(s => s.STREET1);
                break;
            case "addr_desc":
                geocodes = geocodes.OrderByDescending(s => s.STREET1);
                break;
            case "Site":
                geocodes = geocodes.OrderBy(s => s.CFN_SITE);
                break;
            case "site_desc":
                geocodes = geocodes.OrderByDescending(s => s.CFN_SITE);
                break;
            case "City":
                geocodes = geocodes.OrderBy(s => s.CITY);
                break;
            case "city_desc":
                geocodes = geocodes.OrderByDescending(s => s.CITY);
                break;
            case "State":
                geocodes = geocodes.OrderBy(s => s.STATE_CODE);
                break;
            case "state_desc":
                geocodes = geocodes.OrderByDescending(s => s.STATE_CODE);
                break;

            default:
                geocodes = geocodes.OrderBy(s => s.NAME);
                break;
        }
        int pageSize = 20;
        int pageNumber = (page ?? 1);
        return View(geocodes.ToPagedList(pageNumber, pageSize));
    }

И мой взгляд выглядит так:

 @model PagedList.IPagedList<Intranet.Models.CardlockSiteMgr.CFN_Geocodes>
@using PagedList.Mvc

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Site Manager";
}

<h2>Site Manager</h2>

<h5>@Html.ActionLink("Add New Site", "Create")</h5>

@using (Html.BeginForm())
{
    <table class="table table-responsive table-condensed">
        <tr>
            <td colspan="6"><h4>Search Sites</h4></td>
        </tr>
        <tr>
            <td>
                Site:
            </td>
            <td>
                @Html.TextBox("Site")
            </td>
            <td>
                Account:
            </td>
            <td>
                @Html.TextBox("Acct")
            </td>
            <td>
                Name:
            </td>
            <td>
                @Html.TextBox("Name")
            </td>
        </tr>
        <tr>
            <td>
                Address:
            </td>
            <td>
                @Html.TextBox("Address")
            </td>
            <td>
                City:
            </td>
            <td>
                @Html.TextBox("City")
            </td>
            <td>
                State:
            </td>
            <td>
                @Html.TextBox("State")
            </td>
        </tr>
        <tr>
            <td>
                Status:

            </td>
            <td>
                <select name="Status">
                    <option></option>
                    <option>Active</option>
                    <option>Inactive</option>
                </select>
            </td>
            <td>
                Type:

            </td>
            <td>
                <select name="Type">
                    <option></option>
                    <option>CFN</option>
                    <option>Fleetwide</option>
                </select>
            </td>
            <td></td>
            <td>
                <input type="submit" value="Search" />
            </td>
        </tr>



    </table>
}

<table class="table table-responsive table-striped">
    <tr>
        <th>
            @Html.ActionLink("Site", "Index", new { sortOrder = ViewBag.SiteSortParm })
        </th>
        <th>
            @Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>
            @Html.ActionLink("Address", "Index", new { sortOrder = ViewBag.AddressSortParm })
        </th>
        <th>
            @Html.ActionLink("City", "Index", new { sortOrder = ViewBag.CitySortParm })
        </th>
        <th>
            @Html.ActionLink("State", "Index", new { sortOrder = ViewBag.StateSortParm })
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CFN_SITE)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NAME)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.STREET1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CITY)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.STATE_CODE)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.SITE_ID })

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

Есть идеи, что я здесь не так делаю?

1 Ответ

0 голосов
/ 08 марта 2019

В итоге я использовал переменные сеанса, хотя это не самый лучший метод.

if (Status != null)
        {
            System.Web.HttpContext.Current.Session["Status"] = Status;
        }

        if (System.Web.HttpContext.Current.Session["Status"] != null)
        {
            Status = System.Web.HttpContext.Current.Session["Status"].ToString();
        }

        if (Type != null)
        {
            System.Web.HttpContext.Current.Session["Type"] = Type;
        }

        if (System.Web.HttpContext.Current.Session["Type"] != null)
        {
            Type = System.Web.HttpContext.Current.Session["Type"].ToString();
        }

        if (Name != null)
        {
            System.Web.HttpContext.Current.Session["Name"] = Name;
        }

        if (System.Web.HttpContext.Current.Session["Name"] != null)
        {
            Name = System.Web.HttpContext.Current.Session["Name"].ToString();
        }

        if (Site != null)
        {
            System.Web.HttpContext.Current.Session["Site"] = Site;
        }

        if (System.Web.HttpContext.Current.Session["Site"] != null)
        {
            Site = System.Web.HttpContext.Current.Session["Site"].ToString();
        }

        if (Acct != null)
        {
            System.Web.HttpContext.Current.Session["Acct"] = Acct;
        }

        if (System.Web.HttpContext.Current.Session["Acct"] != null)
        {
            Acct = System.Web.HttpContext.Current.Session["Acct"].ToString();
        }

        if (City != null)
        {
            System.Web.HttpContext.Current.Session["City"] = City;
        }

        if (System.Web.HttpContext.Current.Session["City"] != null)
        {
            City = System.Web.HttpContext.Current.Session["City"].ToString();
        }

        if (State != null)
        {
            System.Web.HttpContext.Current.Session["State"] = State;
        }

        if (System.Web.HttpContext.Current.Session["State"] != null)
        {
            State = System.Web.HttpContext.Current.Session["State"].ToString();
        }

        if (Address != null)
        {
            System.Web.HttpContext.Current.Session["Address"] = Address;
        }

        if (System.Web.HttpContext.Current.Session["Address"] != null)
        {
            Address = System.Web.HttpContext.Current.Session["Address"].ToString();
        }
...