Jquery 页面列表分页不工作Mvc4
我收到的问题是,当我选择一个类别时,不会显示任何内容。没有项目没有分页 使用PagedList遵循Troy Goodes的示例,但无法使其工作。jQuery加载三个局部视图(category、items.descripp),我正在尝试对项目结果进行分页 这是我的控制器Jquery 页面列表分页不工作Mvc4,jquery,asp.net-mvc-4,pagination,Jquery,Asp.net Mvc 4,Pagination,我收到的问题是,当我选择一个类别时,不会显示任何内容。没有项目没有分页 使用PagedList遵循Troy Goodes的示例,但无法使其工作。jQuery加载三个局部视图(category、items.descripp),我正在尝试对项目结果进行分页 这是我的控制器 public ActionResult Partial( int id, int? page) { var Uid = WebSecurity.GetUserId(User.Identity.Name)
public ActionResult Partial( int id, int? page)
{
var Uid = WebSecurity.GetUserId(User.Identity.Name);
var pageNumber = page ?? 1;
var query = from i in db.Items
where i.user_id == Uid && i.catId == id
select i;
var results = query;
var onePageOfProducts = results.ToPagedList(pageNumber, 10);
return PartialView("_Partial1", onePageOfProducts);
}
看法
@使用IPagedList;
@使用PagedList.Mvc;
@*@foreach(模型中的var项目)
{*@
@foreach(ViewBag.OnePageOfProducts中的var项){
@item.item\u名称
}
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts,
page=>Url.Action(“/Item/Partial”,新建{page}))
jQuery
$('#categories').load("/Category/GetCats", function () {
$(this).on("click","tr", function () {
var requestpage = "/Item/Partial?id=" + $(this).data("id");
//alert(requestpage); //debug
$.get(requestpage, function (result) {
$("#results").html(result);
});
});
});
$('#results').load("/Item/Partial", function () {
var buttons =
$("<button class=\"removeBtn\">Remove</button>
<button class=\"removeBtn\">Edit</button>");
$(this).on("click", "tr", function () {
var requestpage = "/Item/FullInfoPartial?id=" + $(this).data("id");
buttons.appendTo(this);
//alert(requestpage); //debug
$.get(requestpage, function (result) {
$("#descrip").html(result);
});
});
});
$('#categories').load(“/Category/GetCats”,函数(){
$(此)。在(“单击”,“tr”,函数(){
var requestpage=“/Item/Partial?id=“+$(this).data(“id”);
//警报(请求页面);//调试
$.get(请求页面,函数(结果){
$(“#结果”).html(结果);
});
});
});
$(“#结果”).load(“/Item/Partial”,函数(){
变量按钮=
$(“删除
编辑“);
$(此)。在(“单击”,“tr”,函数(){
var requestpage=“/Item/FullInfoPartial?id=“+$(this).data(“id”);
按钮。附加到(本);
//警报(请求页面);//调试
$.get(请求页面,函数(结果){
$(“#descrip”).html(结果);
});
});
});
要给你一个详细的答案,我应该看看你收到的问题,因为你没有提到到底出了什么问题。无论如何,我已经看到你的代码中存在一些问题
- 您正在调用
,但您没有在ActionResult中创建此ViewBag。请按如下方式修改它:ViewBag.OnePageOfProducts
public ActionResult Partial( int id, int? page) { var Uid = WebSecurity.GetUserId(User.Identity.Name); var pageNumber = page ?? 1; var query = from i in db.Items where i.user_id == Uid && i.catId == id select i; var results = query; var onePageOfProducts = results.ToPagedList(pageNumber, 10); ViewBag.OnePageOfProducts = onePageOfProducts; //* You've fogotten to add this line return PartialView("_Partial1", onePageOfProducts); }
- 您正在使用id参数,但在@Html.PagedListPager帮助程序中忘记了它。如果不定义它,PagedList将从第二页开始丢失结果。因此,请修改它:
@Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("/Item/Partial", new { page = page, id = item.ID})
new{page=page,id=item.id})
item在当前上下文中不存在。如何定义它?@healix它只是一个示例。在您的模型中它是catId。所以写:new{page=page,id=item.catId})
@Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts,
page => Url.Action("/Item/Partial", new { page = page, id = item.ID})