如何在MVC4排序中发送当前页码?

时间:2016-09-22 07:00:41

标签: asp.net-mvc asp.net-mvc-4 paging

您好我已经在我的MVC应用程序中实现了分页,我正在实现分类工具。当我点击标题(ActionLink)时,我想排序。这是我的寻呼机代码。

  @Html.PagedListPager(Model.logDetails, page => Url.Action("Index",
new { page, currentFilter = ViewBag.CurrentFilter, filterdateTime=ViewBag.filterdateTime, filterdocType= Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId, sortOrder = ViewBag.currentSort }))
                Page @(Model.logDetails.PageCount < Model.logDetails.PageNumber ? 0 : Model.logDetails.PageNumber) of @Model.logDetails.PageCount

这是我想要排序的标题。

 <th>@Html.ActionLink("Label", "Index", new { sortOrder = ViewBag.LabelSortParm, currentFilter = ViewBag.CurrentFilter, filterdateTime = ViewBag.filterdateTime, filterdocType = Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId, Page })</th>

这里我遇到将当前页码发送回动作方法的问题。除了当前页码之外,我能够将所有值发送回操作方法。任何人都可以告诉我如何在ActionLink中获取和发送pagenumber?先感谢您。

1 个答案:

答案 0 :(得分:1)

在你的剃刀视图页面的底部,它应该像页面导航一样。在这种情况下,请注意名为page的参数。当从UI发出导航请求时,会将此当前计数传递给Controller Action结果方法。

<!--Grid / Page navigations goes here-->
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("List", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

您应该将控制器逻辑与此类似。这里还要看一下名为page的参数。它始终包含当前页码。当我们从UI传递。默认情况下,我将为0或1。

    public ViewResult List(string sortOrder, string currentFilter, string searchString, int? page, ManageMessage? message)
    {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
            if (searchString != null) { page = 1; }
            else { searchString = currentFilter; }

            ViewBag.CurrentFilter = searchString;

            var company = getComoanyList();

            switch (sortOrder)
            {
                case "Name_desc":
                    company = company.OrderByDescending(s => s.Name);
                    break;
                default:
                    company = company.OrderBy(s => s.Name);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(company.ToPagedList(pageNumber, pageSize));
    }

希望这有帮助。