【发布时间】:2018-01-19 00:04:43
【问题描述】:
我正在使用x.PagedList 在我的 ASP.NET MVC 页面中使用分页。我对插件的唯一问题是,当我在页面之间导航时它使用了页面刷新。
为了避免我使用 jQuery 调用来替换页面内容,如 article 中所述。
我的视图和 javascript 看起来像这样。
<div id="circuitsContent">
<table class="table">
<tr>
--Header
</tr>
@foreach (var item in Model)
{
<tr>
--Loop through and create content
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
</div>
<div id="circuitContentPager">
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Circuits", new { page }))
</div>
@section scripts
{
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$(document).on("click", "#circuitContentPager a[href]", function () {
$.ajax({
url: $(this).attr("href"),
type: 'GET',
cache: false,
success: function (result) {
$('#circuitsContent').html(result);
}
});
return false;
});
});
</script>
这是我的控制器代码:
public ActionResult Circuits(int? page)
{
var pageNumber = page ?? 1;
var circuits = _repo.GetAllCircuits().OrderBy(circ=>circ.ID).ToList();
var pagedCircuits = circuits.ToPagedList(pageNumber, 25);
return View(pagedCircuits);
}
我在这里错过了什么?
【问题讨论】:
-
首先,您应该返回
PartialViewResult,而不是ViewResult -
您的代码还有其他问题 - 您需要使用不同的方法来返回结果(与呈现初始视图的方法不同),以便部分仅呈现表格的 html .
标签: javascript jquery asp.net ajax asp.net-mvc