在我看来,最好的方式总是使用 ajax!性能提升
您的控制器代码:
public class MyCutomModel
{
public int Id { get; set; }
public string Name { get; set; }
}
public class HomeController : Controller
//
// GET: /Home/
public ActionResult Index(int page = 1)
{
List<MyCutomModel> model = new List<MyCutomModel>();
for (int i = 0; i < 10; i++)
{
model.Add(new MyCutomModel { Id = i, Name = "Name " + i.ToString() });
}
if (Request.IsAjaxRequest())
{
return PartialView("_Index", model.ToPagedList(page, 4));
}
return View(model.ToPagedList(page, 4));
}
}
您的索引视图:
@model PagedList<MVCApp.Controllers.MyCutomModel>
@{
ViewBag.Title = "Index";
}
@DateTime.Now
@Html.Partial("_Index", Model)
您的索引部分视图(“_Index.cshtml”):
@model PagedList<MVCApp.Controllers.MyCutomModel>
<div id="replaceDiv">
<table class="table">
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
</tr>
}
</tbody>
</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "replaceDiv" }))
</div>
注意 PagedListPager 的结尾,这是秘密
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "replaceDiv" }))