PagedList在第二页上丢失了搜索过滤器

时间:2013-09-10 14:11:23

标签: c# asp.net asp.net-mvc entity-framework pagedlist

我正在使用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 }) )
}

我的控制器:

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都是空的。

罗比

4 个答案:

答案 0 :(得分:20)

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

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

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

答案 1 :(得分:2)

您好我想通了,使用Tempdata来保存搜索参数。使用某些值调用搜索方法时,请将值存储在tempdata中。当页面列表调用第2页的方法时,从TempData中收集搜索参数。

见:

            if (SearchParameter != null)
            {
                TempData["HoldSearch"] = SearchParameter;
                TempData.Keep();
            }
            else
            {
                SearchParameter  = (CastBacktoType)TempData["HoldSearch"];
                TempData.Keep();
            }

试过这个,效果很好

答案 2 :(得分:2)

如果您有一个包含多个字段的复杂搜索/过滤部分,则可能需要使用以下内容:

<div class="pagedList">
   @Html.PagedListPager(Model, page => Url.Action("Index", new { 
     page, sortOrder = ViewBag.CurrentSort,
     currentFilter = ViewBag.CurrentFilter,
     filter2= Request.QueryString["filter2"],
     filter3= Request.QueryString["filter3"],
     filter4= Request.QueryString["filter4"],
     filter5= Request.QueryString["filter5"] }))

   Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>

这对我有用,现在我可以使用复杂的过滤并在多个页面上查看结果。

答案 3 :(得分:0)

要通过应用多个过滤器来过滤数据,我们可以在 View 中使用这样的 ViewBag。 例如,如果我们要为三个字段(即 EmpFirstName、EmpLastName、EmpLocation)应用过滤器。

@Html.PagedListPager(Model, page => Url.Action("Index", new { 
                           page,
                           sortOrder = ViewBag.sortOrder,
                           currentFilter1 =ViewBag.CurrentFilterForEmpFirstName,
                           currentFilter2 =ViewBag.CurrentFilterForEmpLastName,
                           currentFilter3 =ViewBag.CurrentFilterForEmpLocation}))

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

在控制器中我写了这样的代码:

public ActionResult Index( int ?page, string currentFilter1, string currentFilter2, string currentFilter3,
 string searchEmpFirstName , string searchEmpLastName, string searchEmpLocation)

int pageSize = 10;
int pageNumber = (page??1);

var emp_master = db.Emp_Master.Include(l => l.Emp_Location);
   if (searchEmpFirstName != null || searchEmpLastName != null || searchEmpLocation != null)
   {
      page = 1;
   }
   else
   {
      searchEmpFirstName = currentFilter1;
      searchEmpLastName = currentFilter2;
      searchEmpLocation = currentFilter3;
   }

   ViewBag.CurrentFilterForEmpFirstName = searchEmpFirstName;
   ViewBag.CurrentFilterForEmpLastName = searchEmpLastName;
   ViewBag.CurrentFilterForEmpLocation = searchEmpLocation;

  if(!String.IsNullOrEmpty(searchEmpFirstName))
  {
      emp = emp.Where(s => s.ModelName == searchEmpFirstName)
  }
  if(!String.IsNullOrEmpty(searchEmpLastName))
  {
      emp = emp.Where(s => s.ModelName == searchEmpLastName)
  }
  if(!String.IsNullOrEmpty(searchEmpLocation))
  {
      emp = emp.Where(s => s.ModelName == searchEmpLocation)
  }