我如何转换我的分页功能使用AJAX欧洲工商管理学院? [英] How Can I Convert My Paging Functionality To Use AJAX Insead?

查看:147
本文介绍了我如何转换我的分页功能使用AJAX欧洲工商管理学院?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想转换此code。使用Ajax和新的结果而不是刷新整个页面来更新页面。我真的不知道如何去说。林相当新的MVC从Web表单的到来。任何帮助将大大AP preciated!

我的继承人code:

家庭控制器:

  // #################################### #
//的ActionResult =找回评论
// #####################################
[ChildActionOnly]
公众的ActionResult _comments(INT的ProductID,诠释?页)
{
    //执行数据库查询。
    尝试
    {
        // SQL查询 - 获取*记录
        VAR模型=从R IN DB.Comments那里r.ProductID ==的ProductID选择R;        //页码通过URL传递。默认为1,如果没有规定。
        VAR的PageNumber =页面? 1;        //抓斗从结果集6的结果。
        变种结果= Model.ToPagedList(的PageNumber,6);        //存储分页结果在ViewBag为寻呼集。
        ViewBag.Results =结果;        在ViewBag //商店显示(43页1)
        ViewBag.PageNumber =的PageNumber;        //获取总页数:每页除以总记录6条记录。
        ViewBag.PageCount = Model.Count()/ 6 + 1;        //返回记录我的看法。
        返回PartialView(结果);
    }
    //有一个错误。
    抓住
    {
        //ViewBag.ErrorMessage =前;
        返回PartialView(错误);
    }
}

PartialView:_comments

  @model IEnumerable的< D​​H.Models.Comment>@using PagedList.Mvc;
@using PagedList;@ {
    //没有评论
    如果(@ Model.Count()== 0)
    {
        < D​​IV ID =CommentStatus>成为第一个该产品发表评论<!/ DIV>
    }
    //有评论!
    其他
    {
        的foreach(在型号VAR项)
        {
             // HTML写出的评论......
        }
    }
}< D​​IV> @ ViewBag.PageCount&LT的页面@ ViewBag.PageNumber; / DIV>@ Html.PagedListPager((IPagedList)ViewBag.Results,页​​=> Url.Action(指数,新的{页=页}),新PagedListRenderOptions {LinkTo previousPageFormat =< preV, LinkToNextPageFormat =下一页>中,LinkToLastPageFormat =&放大器; Uacute; ltima>>})

示范

 命名空间DH.Models
{
    公共类评论
    {
        公众诠释CommentID {搞定;组; }        公众诠释的ProductID {搞定;组; }        公共字符串作者{搞定;组; }        公共字符串消息{搞定;组; }        公众的DateTime MessageDate {搞定;组; }        公众诠释ThumbsUp {搞定;组; }        公众诠释ThumbsDown {搞定;组; }        公共字符串IP {搞定;组; }
    }
}


解决方案

我是PagedList的NuGet包的创造者。

有做的Ajax分页的该项目的混帐回购协议的例子(以及无限滚动):

https://github.com/TroyGoode/PagedList/树/主/ src目录/ PagedList.Mvc4.Example

控制器:
<一href=\"https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc4.Example/Controllers/AjaxController.cs\" rel=\"nofollow\">https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc4.Example/Controllers/AjaxController.cs

查看:
<一href=\"https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc4.Example/Views/Ajax/Index.cshtml\" rel=\"nofollow\">https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc4.Example/Views/Ajax/Index.cshtml

I currently have pagination working in my MVC 3 project using the PagedList library (https://github.com/TroyGoode/PagedList).

I would like to convert this code to update the page using ajax with the new results rather then refreshing the whole page. I'm not really sure how to go about that. Im rather new to MVC coming from Webforms. Any help would be greatly appreciated!

Heres my code:

Home 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");
    }
}

PartialView: _Comments

@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 >>" })

Model

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; }
    }
}

解决方案

I'm the creator of the PagedList nuget package.

There is an example of doing Ajax pagination (as well as infinite scrolling) in the project's git repo:

https://github.com/TroyGoode/PagedList/tree/master/src/PagedList.Mvc4.Example

Controller: https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc4.Example/Controllers/AjaxController.cs

View: https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc4.Example/Views/Ajax/Index.cshtml

这篇关于我如何转换我的分页功能使用AJAX欧洲工商管理学院?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆