本文介绍了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

我的问题是,搜索字符串丢失当我页,第二页左右,而不是一个过滤的结果,我显示的所有记录。

My problem is that the search string is 'lost' when I page to the second page, so instead of a filtered set of results, I'm shown all the records.

我index.cshtml:

My 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和搜​​索字符串均为空。

When I go to Page 2 as an example, all my variables, sortOrder, currentFilter and searchString are all null.

罗比

推荐答案

问题是你PagedList项不包括您的排序顺序也不是你当前的过滤器。

The problem is your PagedList entry doesn't include your sort order nor your current filter.

在除了添加ViewBag.CurrentSort由Vasanth的建议,你也需要你的PagedListPager更改为:

In addition to adding ViewBag.CurrentSort as suggested by Vasanth, you also need to change your PagedListPager to:

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

这篇关于PagedList失去搜索过滤器第二页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:54