18

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

4 回答 4

23

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

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

@Html.PagedListPager( Model, page => Url.Action("Index", new { page, currentFilter=ViewBag.CurrentFilter, sortOrder = ViewBag.sortOrder}) )
于 2013-09-11T10:56:06.457 回答
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>

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

于 2017-01-06T10:58:30.867 回答
2

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

看到这个:

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

这个试过了,效果很好

于 2016-11-08T14:43:25.930 回答
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)
  }
于 2021-04-19T17:50:04.733 回答