PagedList在第二页及其后的页面上丢失搜索过滤器

时间:2018-07-23 08:26:32

标签: c# asp.net-mvc pagedlist

我丢失了第二页上的所有数据,此后,第一页结果显示正确的数据。第二页及其后,没有数据。

我的代码:

public ActionResult Contact(int? pageNumber, string search, string option, string select)
    {
        if (option == "Company")
        {            
            return View(db.CompaniesServers.Where(x => x.Name.Contains(search)).ToList().ToPagedList(pageNumber ?? 1, 2));
        }
        else
        {
            return View(db.CompaniesServers.ToList().ToPagedList(pageNumber ?? 1, 3));
        }
    }

我的视图:

@using PagedList;
@using PagedList.Mvc;

@model IPagedList<WorkingWithData.Models.CompaniesServer>


@using (Html.BeginForm("Contact", "Home", FormMethod.Get))
            {

    <b>
        Search Options: <br />
    </b>@Html.RadioButton("option", "Company") <text>Company Name</text> 
    @Html.TextBox("search", null, new { style = "height:30px" })  <select id="serverList" style="height:33px; font-size:small" class="btn btn-default">
                    <option value="All">Select Server</option>
                    <option value="10.67.21.40">Lam Server 4</option>
                    <option value="10.67.21.47">Lam Server 14</option>
                    <option value="10.67.21.70">Lam Server 12</option>
                </select> <a class="btn btn-default" href="/Home/Contact">Reset</a>    <input type="submit" name="submit" value="Search" class="btn btn-primary" />
                
}

<table class="table">
    <tr>
        <th>
           Address
        </th>
        <th>
           Port
        </th>
        <th>
           Name
        </th>
        <th>
          Environment
        </th>
        <th>
           Active
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Port)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Environment)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Active)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

@Html.PagedListPager(Model, pageNumber => Url.Action("Contact", new
{
    pageNumber
}))

首页结果返回有效信息。但是,当我进入第二页时,什么也没有出现。调试时,我的变量返回null。

我应该尝试停止页面加载吗? MVC的新功能。

2 个答案:

答案 0 :(得分:3)

我的解决方法是在控制器中使用ViewBag,但不确定如何在代码中实现它,但可能会有所帮助

查看:

MAX

控制器:

SELECT MAX(gameweek) FROM `match` m WHERE round_id = 1 AND m.status = 3

答案 1 :(得分:2)

我找到了答案。我确实不想使用.env,但必须这样做。似乎我需要在每次分页时存储/发送回我的“ option”和“ search”值。起初,我只存储“ pageNumber”。

已添加到我的控制器中:

ViewBag

进入视图,添加到 ViewBag.Search1 = option; ViewBag.Search2 = search; 的分页部分:

PagedListPager