您好,我已经在我的 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 中获取和发送页码?先感谢您。
在剃刀视图页面的底部,应该有类似的内容用于页面导航。在这种情况下,请注意名为
page
的参数。当从 UI 发出导航请求时,会将当前计数传递给控制器操作结果方法。
<!--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 过去时。默认情况下,i 将为 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));
}
希望这有帮助。