1

要执行分页,我遵循了这个方法 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-在-an-asp-net-mvc-应用程序中

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

 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));
    } 

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

4

1 回答 1

0

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

于 2014-11-13T12:17:52.680 回答