c# - 如何转换我的分页功能以使用 AJAX Insead?

标签 c# asp.net asp.net-mvc-3 pagination

我目前在我的 MVC 3 项目中使用 PagedList 库 (https://github.com/TroyGoode/PagedList) 进行分页。

我想转换此代码以使用带有新结果的 ajax 更新页面,而不是刷新整个页面。我不太确定该怎么做。我对来自 Webforms 的 MVC 很陌生。任何帮助将不胜感激!

这是我的代码:

家庭 Controller :

//#####################################
// ActionResult = Retrieve Comments
//#####################################
[ChildActionOnly]
public ActionResult _Comments(int ProductID, int? Page)
{
    //Perform the Database Query.
    try
    {
        //SQL Query - Get * records
        var Model = from r in DB.Comments where r.ProductID == ProductID select r;

        //Page number passed in via url. Default to 1 if not specified.
        var PageNumber = Page ?? 1;

        //Grab 6 results from the result set.
        var Results = Model.ToPagedList(PageNumber, 6);

        //Store Paged Result set in ViewBag for Paging.
        ViewBag.Results = Results;

        //Store in ViewBag for display (Page 1 of 43)
        ViewBag.PageNumber = PageNumber;

        //Get Total Pages: Divide Total Records by 6 Records per page.
        ViewBag.PageCount = Model.Count() / 6 + 1; 

        //Return Records to my view.
        return PartialView(Results);
    }
    //There was an error.
    catch
    {
        //ViewBag.ErrorMessage = ex;
        return PartialView("Error");
    }
}

局部 View :_评论

@model IEnumerable<DH.Models.Comment>

@using PagedList.Mvc;
@using PagedList;

@{  
    //No Comments Yet
    if (@Model.Count() == 0)
    {
        <div id="CommentStatus">Be the first to comment on this product!</div>
    }
    //There are comments!
    else
    {
        foreach (var item in Model)
        {   
             //html for writing out the comments...
        }
    }
}

<div>Page @ViewBag.PageNumber of @ViewBag.PageCount</div>

@Html.PagedListPager((IPagedList)ViewBag.Results, Page => Url.Action("Index", new { Page = Page }), new PagedListRenderOptions { LinkToPreviousPageFormat = "< Prev", LinkToNextPageFormat = "Next >", LinkToLastPageFormat = "&Uacute;ltima >>" })

模型

namespace DH.Models
{
    public class Comment
    {  
        public int CommentID { get; set; }

        public int ProductID { get; set; }

        public string Author { get; set; }

        public string Message { get; set; }

        public DateTime MessageDate { get; set; }

        public int ThumbsUp { get; set; }

        public int ThumbsDown { get; set; }

        public string IP { get; set; }
    }
}

最佳答案

关于c# - 如何转换我的分页功能以使用 AJAX Insead?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559807/

相关文章:

c# - ServiceStack 与 NServiceBus

c# - 如何阻止 1 个解决方案启动您的所有网站?

javascript - 在 asp.net 中隐藏更新面板中的 div

javascript - 如何正确设置标签和文本框填充?

jquery - MVC3 Action 链接 - 触发 Jquery 的图像按钮

c# - 如何在 gridView asp.net 中制作 X 滚动?

c# - 空对象检查

javascript - 自动点击/强制触发js功能

c# - 领域驱动设计、包含实体和 NHibernate 持久性

asp.net-mvc - MVC3 中的渲染导航