标签:
public ActionResult Browse(string id)
{
using (musicstoreEntities db = new musicstoreEntities())
{
ViewBag.GenreId = id;
int count = db.Albums.Count(a => a.GenreId.ToString() == id);
int pageCount = 0;
if (count%10 == 0)
{
pageCount = count/10;
}
else
{
pageCount = count/10 + 1;
}
ViewBag.AlbumCount = pageCount;
return View();
}
}
public ActionResult PageList(string id, int pageNum, int pageSize)
{
List<string> albumTitleList = null;
using (musicstoreEntities db = new musicstoreEntities())
{
albumTitleList =
db.Albums.Where(album => album.GenreId.ToString() == id).OrderBy(album => album.Title)
.Skip((pageNum - 1) * pageSize)
.Take(pageSize)
.ToList().Select(album => album.Title).ToList();
}
return Json(albumTitleList, JsonRequestBehavior.AllowGet);
}
<script>
$(function() {
//$.ajaxSetup({ cache: false });
var pageCount = Number($("#pageCount").val());
loadPageList(1, 10, pageCount);
});
function loadPageList(pageNum, pageSize, pageCount) {
var genreid = $("#genreid").val();
$.get("/Store/PageList", { id: genreid, pageNum: pageNum, pageSize: pageSize }, function(data) {
$("#ulAlbumList").empty();
$(".pagination").empty();
$.each(data, function(index, item) {
$("#ulAlbumList").append("<li>" + item + "</li>");
});
$(".pagination").append(‘<li><span aria-label="Previous"><span aria-hidden="true">«</span></span></li>‘);
for (var i = 1; i <= pageCount; i++) {
$(".pagination").append("<li><span>" + i + "</span></li>");
if (pageNum === i) {
$(".pagination li:eq(" + i + ")").addClass("active");
}
}
$(".pagination").append(‘<li><span aria-label="Next"><span aria-hidden="true">»</span></span></li>‘);
if (Number(pageNum) === Number(pageCount)) {
$(".pagination li:last").addClass("disabled");
}
if (Number(pageNum) === 1) {
$(".pagination li:first").addClass("disabled");
}
getNextPageList(pageNum, pageSize, pageCount);
getPrePageList(pageNum, pageSize, pageCount);
});
}
function getNextPageList(pageNum, pageSize, pageCount) {
$(".pagination li:last").click(function () {
loadPageList(Number(pageNum) + Number(1), pageSize, pageCount);
});
}
function getPrePageList(pageNum, pageSize, pageCount) {
$(".pagination li:first").click(function() {
loadPageList(Number(pageNum) - Number(1), pageSize, pageCount);
});
}
</script>
标签:
原文地址:http://www.cnblogs.com/newstar87/p/5436989.html