MVC中的分页列表

时间:2013-04-17 16:47:58

标签: asp.net-mvc-3 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

现在我要做的是通过从下拉列表中选择一个值来更改页面大小

 Page Sige :
           @{
             IList<SelectListItem> pageSizeList = new List<SelectListItem>();
             pageSizeList.Add(new SelectListItem { Text = "5", Value = "5", Selected = true });
             pageSizeList.Add(new SelectListItem { Text = "10", Value = "10" });
             pageSizeList.Add(new SelectListItem { Text = "15", Value = "15" });
             pageSizeList.Add(new SelectListItem { Text = "20", Value = "20" });
             pageSizeList.Add(new SelectListItem { Text = "25", Value = "25" });
             }
             @Html.DropDownListFor(per => per.PageSize, pageSizeList, new
             {
                @onchange = "location.href='/AdminSearchResult/AdminSearchResult?pageSizeInput=' + this.value;"
             })

从下拉列表中选择值后,我对排序和分页操作链接进行了一些更改。我添加了一个bool参数isPageSort,如下所示

@Html.ActionLink("  Previous Page  ", "AdminSearchResult", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, isSortPage = "true"  }) -------For paging

@Html.ActionLink("Last Name", "AdminSearchResult", new { sortOrder = ViewBag.LastNameSortParm, currentFilter = ViewBag.CurrentFilter, isSortPage = "true" }) ------For sorting

基于此bool参数,我在会话中保存当前页面大小并检索以维护控制器中的页面大小。

[HttpGet]
    public ActionResult AdminSearchResult(string sortOrder, int? page, int pageSizeInput = 0,  bool isSortPage = false)
    {
        int pageSize = 5;         
        int pageNumber = 1;
        if (page != null)
            pageNumber = Convert.ToInt32(page);


        if (!isSortPage)
        {
            if (pageSizeInput != 0)
                pageSize = pageSizeInput;
            Session["pageSize"] = pageSize;
        }
        else
            pageSize = (int)Session["pageSize"];

        Session["sortOrder"] = sortOrder;
        Session["pageNumber"] = pageNumber;

        var userList = SortResult(sortOrder);

        return View(userList.ToPagedList(pageNumber, pageSize));
    } 

我想知道是否有更有效的方法来做同样的事情。

1 个答案:

答案 0 :(得分:0)

@dotnetBeginner您可以使用MVC中的分页列表,它比这更灵活....