asp.net MVC implementation without refreshing paging based on Jquery+ajax

Source: Internet
Author: User

I wrote a demo using the ASP.net MVC framework, the paging feature is implemented by Jquery+ajax without refreshing paging.

Solution idea: The initial loading of page data and page loading are done through AJAX, page paging link after clicking the use of AJAX technology to send the current page number to the back-end controller, the back-end controller based on the current page number and settings pagesize from the database to remove the corresponding page of data. After the back-end controller is processed, the data is returned to the partial view using the Partialview method, and the viewbag is used to return the current page number and total records and pagesize. Ajax uses the callback function to add the view returned by the controller to the page.

Description: Paging specific page navigation and style using Plug-ins.

Controller code

Using _116.models;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;

Using SYSTEM.WEB.MVC; Namespace _116.controllers {public class Homecontroller:controller {private DataContext db = new Datac

        Ontext ();
        Private readonly int pageSize = 3;
            Get:/home/public actionresult Index () {viewbag.pagesize = PageSize; Viewbag.totalcount = db.
            Articles.count ();
        return View (); ///<summary>///articles list, Ajax paging///</summary>///<param name= "PageIndex"
        ></param>///<returns></returns> public actionresult ajaxpaging (int pageIndex = 1) {iqueryable<articlemodels> articles = db. Articles.orderbydescending (a => a.objectid). Skip ((pageIndex-1) * pageSize).

            Take (pageSize);
            
            Viewbag.pageindex = PageIndex; ReTurn Partialview ("Indexarticlelist", articles);
 }
    }
}
View Code
<!--articles List-->
    <section id= "articlelist" class= "posts block" ></section>
<!--page-->
<script src= "~/common/scripts/kkpager.js" ></script>
<link href= "~/common/plugincss/" Kkpager.css "rel=" stylesheet "/> <nav id=" Kkpager "class=
" posts block pagination "></nav>
&L t;! --Pagination-->

Partial View Code
@model ienumerable<_116.models.articlemodels> @foreach (var item in model) {<article class= ' post block '
        ; <!--title--> 

Ajax Code

<script type= "Text/javascript" > $ (function () {///initial load article List data getarticlesdata (1);

    }); Ajax get a list of articles and page function Getarticlesdata (pageIndex) {var ajaxurl = '/home/ajaxpaging?pageindex= ' + Pagein
        Dex
        var ajaxtype = ' post ';
        var ajaxdatatype = ' text ';
                var sucfun = function (data, status) {if (data = null && status!= "Success") { Alert ("Get data failed.")
                ");
            return false;

                else {$ (' #articleList '). HTML (data);

                Removes the bottom shadow $ ("#articleList") of the last item in the list. Find ("article"). ("Removeclass").
                paging var totalcount = parseint (' @ViewBag. TotalCount ');
                var pageSize = parseint (' @ViewBag. pageSize '); var totalpages = totalcount% pageSize = 0?
           Totalcount/pagesize: (parseint (totalcount/pagesize) + 1);     Pagecutforsearch (TotalPages, TotalCount, $ ("#pageIndex"). Val ());

        }
        }; Ajax parameter Set var Option = {Url:ajaxurl, type: Ajaxtype, Datatype:ajaxdatatype, Cache:false,//set to False will not load the request from the browser cache
                        Information. Async:true,//(default: TRUE), all requests are asynchronous requests. To send a sync request, set this option to false.
                        The synchronization request will lock the browser and the user's other actions must wait for the request to complete before it can be executed. timeout:3600,//Set request timeout (milliseconds).
                        This setting overrides the global setting.
                    Error:function () {}, Success:sucfun, Beforesend:function () {}
        };
        $.ajax (Option);
    return false;
    //ajax Paging function Searchpage (n) {getarticlesdata (n); } </script>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.