我想使用PagedList.MVC库进行分页。 但是当我单击鼠标并单击到PageNumber时,我看到列表重新绑定但数据不受影响,因为param仍然是页面1。为什么PageNumber总是1。我已附加了图像。
我使用的是Microsoft.jQuery.Unobtrusive.Ajax 3.2.5,PagedList.Mvc 4.5和MVC5。我不知道哪些不兼容。
这是我在ViewPartial中的分页代码:
@if (Model.TotalItemCount > 5)
{
<div class="pagingBox">
<div class="pagerecord display_none_mobile">
Records
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>
@Html.PagedListPager(Model, page => Url.Action("Index", "Customer",
new
{
pageNumber = ViewBag.PageNumber,
pageSize = ViewBag.PageSize
}),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions
{
Display = PagedListDisplayMode.IfNeeded,
MaximumPageNumbersToDisplay = 5
},
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "Get",
UpdateTargetId = "CricketersGrid",
LoadingElementId = "divProcessing"
}))
</div>
<div id="divProcessing" class="processingButton" style="display: none;">
<img width="31" height="31" alt="" class="LoadingImage" src="~/Images/ajax-loader-round.gif" />
</div>
}
此代码在控制器中:
public async Task<ActionResult> Index(int? pageNumber, int? pageSize)
{
if (!pageNumber.HasValue)
pageNumber = 1;
if (!pageSize.HasValue)
pageSize = 5;
int startIndex = pageNumber > 1 ? (pageNumber.Value * pageSize.Value) + 1 : 1;
int endIndex = startIndex + pageSize.Value - 1;
ViewBag.PageNumber = pageNumber;
ViewBag.PageSize = pageSize;
Customers param = new Customers();
List<ViewCustomer> lst = await _cust.GetListPagingAsync(param, startIndex, endIndex);
List<ViewCustomerModel> result = Mapper.Map<List<ViewCustomerModel>>(lst);
return View("~/Views/Customer/Index.cshtml", new ViewCustomerPagingModel() { viewCustomerModel = result });
}
这在View索引中:
@model EBO.Customer.Web.Models.ViewCustomerPagingModel
@using PagedList;
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
@using (Ajax.BeginForm("Index", "Customer", null, new AjaxOptions() { LoadingElementId = "", HttpMethod = "Get", UpdateTargetId = "CricketersGrid" }))
{
<div class="CricketersList">
<div id="CricketersGrid">
@Html.Partial("~/Views/Customer/_PagingPartial.cshtml", new StaticPagedList<EBO.Customer.Web.Models.ViewCustomerModel>(Model.viewCustomerModel, Convert.ToInt32(ViewBag.PageNumber), Convert.ToInt32(ViewBag.PageSize), Model.viewCustomerModel.Count > 0 ? Model.viewCustomerModel.FirstOrDefault().TotalCount : 0))
</div>
</div>
}