ASP.NET MVC Html.PagedListPager不工作

时间:2016-01-28 14:40:05

标签: asp.net-mvc pagedlist

myController的:

    public ActionResult Index(String SearchString, int? page)
    {
        var query = from _mytable in _MyDataContext.MyTables select _mytable;

        int pageSize = 10;              // will only contain 10 products max because of the pageSize
        int pageNumber = (page ?? 1);   // if no page was specified in the querystring, default to the first page (1)

        PagedList.PagedList<MyTables> models = new PagedList.PagedList<Anlagen>(query, pageNumber, pageSize);

        return View(models);
    }

MyView,最后一行:

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

我不知道为什么,但唯一的分页是: «| <| 1 |> |»

使用“?SearchString = hello&amp; page = 2”调用页面可以很好地处理结果,但具有相同的分页: «| <| 1 |> |»

这是我的搜索表单:

<form action="@Url.Action("Index", "Anlagen")" method="get" role="search" class="navbar-form-custom">
    <div class="form-group input-group">
        <input type="text" class="form-control" placeholder="suchen..." id="SearchString" name="SearchString" value="@ViewBag.SearchString" />
    </div>
</form>

只是来自工作项目的副本,但在新项目中它不起作用:(

2 个答案:

答案 0 :(得分:1)

我相信您必须为PagedList订购元素才能正常运作。调整LINQ以按照某个值对您的集合进行排序,例如Name如果存在:

var query = from _mytable in _MyDataContext.MyTables orderby _mytable.Name select _mytable;

答案 1 :(得分:-1)

    This is  best Solution for PAGEDLISTPAGER in mvc5 :

    @Html.PagedListPager(Model,Page => Url.Action("Index",new { Page,    SearchBy=Request.QueryString["SearchBy"],
    Search = Request.QueryString["Search"] }), new PagedListRenderOptions() {Display=PagedListDisplayMode.IfNeeded
    ,DisplayPageCountAndCurrentLocation=true,DisplayItemSliceAndTotal=true
     } )


//////////////////////////////////////////////////////////////////////////////
    ///////////Solution////////browser back button give problem in MVC when logout////////////////////////////////////////////////////////////

    I had this problem a while ago, disabling the cache for the entire application solved my problem, just add these line to the Global.asax.cs file

            protected void Application_BeginRequest()
            {
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
                Response.Cache.SetNoStore();
            }