MVC pagination Mvcpager Use of detailed _ practical skills

Source: Internet
Author: User
Tags prev

Recently contacted MVC soon, because the project to use pagination, the Internet to find the data, and finally adopted the Mvcpager (http://www.webdiyer.com/), support synchronous and Ajax asynchronous paging. Nonsense not to say more directly on the code.

A. Mvcpager asynchronous
ViewModel:

 public class Article
 {
 [Display (Name = ' information number ']] public
 int ID {get; set;}

 [Display (Name = "information title")]
 public string Title {get; set;}

 [Display (Name = "Information content")]
 public string Content {get; set;}
 }

 public class Ajaxpager
 {public
 pagedlist<article> articles {get; set;} 
 } 

Control:

<summary>
 ///Asynchronous paging test
 ///</summary>
 ///<param name= "id" >pageIndex</param>
 ///<param name= "key" > keyword </param>
 ///<returns></returns> public
 ActionResult ajaxpaging (int? id = 1, string key = null)
 {
  int totalcount = 0;
  int pageIndex = ID?? 1;
  int pageSize = 2;
  list<article> infolist = new Solefudal.mytest (). Getarticlelist (Key, PageSize, (pageIndex-1) * 2, out totalcount);
  pagedlist<article> Infopager = infolist.asqueryable (). OrderByDescending (o => o.id). Topagedlist (PageIndex, pageSize);
  Infopager.totalitemcount = TotalCount;
  Infopager.currentpageindex = (int) (ID?? 1);

  Models.MyTest.AjaxPager model = new Models.MyTest.AjaxPager ();
  Model. articles = Infopager;
  if (Request.isajaxrequest ())
  {return
  Partialview ("_articlelist", model);
  }
  return View (model);
 }



View:

@model Soulefu_manage.

Models.MyTest.AjaxPager @using WEBDIYER.WEBCONTROLS.MVC; <! DOCTYPE html>  
@model Soulefu_manage. Models.MyTest.AjaxPager

<table class= "table table-bordered table-striped" >
 <tr>
 <th class= "nowrap" > Serial number </th>
 <th>
  title
 </th>
 <th>
  content
 </th>
 </tr>
 @foreach (var item in model.articles)
 {
 <tr>
  <td>@ Html.displayfor (Model => item.id) </td>
  <td>
  @Html. Displayfor (ModelItem => item. Title)
  </td>
  <td>
  @Html. Displayfor (ModelItem => item. Content)
  </td>
 </tr>
 }
</table>

Two. Mvcpager Sync
ViewModel (not added here, directly and asynchronously share the same):

 public class Mvcpager
 {
 //Information list public
 pagedlist<article> articles {get; set;}
 } 

Control:

 <summary>
 ///Sync paging test
 ///</summary>
 ///<param name= "id" >pageIndex</param>
 ///<param name= "key" > keyword </param>
 ///<returns></returns> public
 ActionResult Mvcpager (int? id = 1, string key = null) 
 {
  int totalcount = 0;
  int pageIndex = ID?? 1;
  int pageSize = 2;
  list<article> infolist = new Solefudal.mytest (). Getarticlelist (Key, PageSize, (pageIndex-1) * 2, out totalcount);
  pagedlist<article> Infopager = infolist.asqueryable (). OrderByDescending (o => o.id). Topagedlist (PageIndex, pageSize);
  Infopager.totalitemcount = TotalCount;
  Infopager.currentpageindex = (int) (ID?? 1);

  Data assembly to ViewModel
  Models.MyTest.MVCPager model = new Models.MyTest.MVCPager ();
  Model. articles = Infopager;
  return View (model);
 }

View:

@model Soulefu_manage.

Models.MyTest.MVCPager @using WEBDIYER.WEBCONTROLS.MVC; <! DOCTYPE html>  

Get test data methods (shared):

 public class MyTest {///<summary>///get test data///</summary>///<param name= "key" ></param&gt
 ; <param name= "PageSize" ></param>///<param name= "Currentcount" ></param>///<param Name = "TotalCount" ></param>///<returns></returns> public list<article> getarticlelist ( string key, int PageSize, int currentcount, out int totalcount) {string tabname = string.
  Format ("Article");
  String strwhere = "1=1"; if (!string. IsNullOrEmpty (key) {//sql keyword filter contains keywords do not stitch SQL if (!) Sqlinjection.getstring (key)) {strwhere = = string.
  Format ("and (Title like '%{0}% ' OR Content like '%{0}% ')", key); } string order = string.
  Format ("ID ASC");
  DataSet ds = Sqlhelper.getlist (sqlhelper.connstr, order, PageSize, Currentcount, TabName, strwhere, out totalcount);
  list<article> list = new list<article> (); if (ds!= null && ds. Tables.count > 0) {foreach (DataRow dr in DS). TaBles[0].
   Rows) {Article model = new Article ();
   Model.id = Convert.ToInt32 (dr["ID")); Model. title = dr["title"].
   ToString (); Model. Content = dr["Content"].
   ToString (); List.
  ADD (model);
 } return list;

 }
 }

Effect diagram: (CSS required)

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.