gpt4 book ai didi

c# - PagedList 在第二页上丢失搜索过滤器

转载 作者:可可西里 更新时间:2023-11-01 07:44:06 25 4
gpt4 key购买 nike

我正在使用 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application 中的示例实现一个简单的分页列表索引

我的问题是,当我翻到第二页时,搜索字符串“丢失”了,所以我看到的不是经过筛选的结果集,而是所有记录。

我的 index.cshtml:

@using (Html.BeginForm("Index", "", FormMethod.Get))
{
<p>
@Html.TextBox("searchString", ViewBag.currentFilter as string, new { @placeholder = "Search by title or author" })
<input type="submit" value="Search" />
</p>
}

@if (Model.PageCount > 1)
{
@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )
}

我的 Controller :

public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.TitleSortParm = sortOrder == "Title" ? "Title desc" : "Title";
ViewBag.AuthorSortParm = sortOrder == "Author" ? "Author desc" : "Author";
ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;

}

ViewBag.currentFilter = searchString;

var Articles = from a in db.Articles
select a;
if (!String.IsNullOrEmpty(searchString))
{
//page = 1;
Insights = Articles.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper())
|| s.Author.ToUpper().Contains(searchString.ToUpper()));
}

switch (sortOrder)
{
case "Author":
Insights = Articles.OrderBy(s => s.Author);
break;
case "Author desc":
Insights = Articles.OrderByDescending(s => s.Author);
break;
case "Title":
Insights = Articles.OrderBy(s => s.Title);
break;
case "Title desc":
Insights = Articles.OrderByDescending(s => s.Title);
break;
case "Date":
Insights = Articles.OrderBy(s => s.DatePublished);
break;
default:
Insights = Articles.OrderByDescending(s => s.DatePublished);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(Articles.ToPagedList(pageNumber, pageSize));

}

当我以第 2 页为例,我所有的变量,sortOrder、currentFilter 和 searchString 都是空的。

罗比

最佳答案

问题是您的 PagedList 条目不包括您的排序顺序或当前过滤器。

除了按照 Vasanth 的建议添加 ViewBag.CurrentSort 之外,您还需要将 PagedListPager 更改为:

@Html.PagedListPager( Model, page => Url.Action("Index", new { page, currentFilter=ViewBag.CurrentFilter, sortOrder = ViewBag.sortOrder}) )

关于c# - PagedList 在第二页上丢失搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18721249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com