【问题标题】:PagedList loses search filter on second page and after pagesPagedList 在第二页和页后丢失搜索过滤器
【发布时间】:2018-12-30 15:38:47
【问题描述】:

我丢失了第二页上的所有数据,然后是。第一个结果/页面显示正确的数据。第二页和之后,没有数据。

我的代码:

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 的新功能。

【问题讨论】:

  • 你指的是什么变量? (您没有在searchoptionselect 的网址中发送任何内容,因此它们将始终为null
  • 如果您想保留搜索/过滤功能,请参考this answer 示例

标签: c# asp.net-mvc pagedlist


【解决方案1】:

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

查看:

@model IPagedList<MyModel>
    <div class="pagedList">
        @Html.PagedListPager(Model, page => Url.Action("Index", new { page, search = ViewBag.search }),
       PagedListRenderOptions.MinimalWithItemCountText)
    </div>

控制器:

public ActionResult Index(string search = null, int page = 1)
        {
            ViewBag.search = search;
            var model = _db.Employees
                .Where(r => search == null || r.Name.StartsWith(search) || r.Name.Contains(search))
                .Select(r => new EmployeeViewModel
                {
                    Id = r.Id,
                    Name = r.Name,
                    City = r.City,
                    Country = r.Country,
                }).ToPagedList(page, 10);
            return View(model);
        }

【讨论】:

  • 谢谢!一看到你的帖子,我就意识到我没有发回我的选项并搜索回我的页面。所以我使用 Viewbag 发送/存储它们。
【解决方案2】:

我找到了答案。我真的不想使用ViewBag,但我不得不这样做。似乎我需要在每次分页时存储/发回我的“选项”和“搜索”值。起初,我只存储“pageNumber”。

在我的控制器中添加:

 ViewBag.Search1 = option;
 ViewBag.Search2 = search;

进入查看,在PagedListPager的分页部分添加:

option = ViewData["Search1"];
search = ViewData["Search2"];

【讨论】:

  • 使用视图模型,而不是 ViewBag(Mochuelo 已经在他们的回答中指出了这一点,因此您应该接受这一点,而不是添加自己的答案)
  • 谁的 Mochuelo?
  • 添加其他答案的用户
  • 我同意 user3559349。您应该接受 Mochuelo 的答案作为正确答案,而不是接受他的帮助,将其改写为您的确切代码并重新发布。
猜你喜欢
  • 2013-09-14
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多