筛选MVC时分页中断

时间:2019-03-07 00:18:29

标签: c# html razor model-view-controller pagination

我正在尝试使分页能够进行多种搜索和排序。当我执行搜索时,返回的第一页是结果集的正确页面,但是当我使用分页按钮选择下一页时,搜索不再受其影响,并且得到所有结果。

我的控制器如下:

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 :(得分:0)

即使不是最佳方法,我还是使用了会话变量。

{{1}}