ASP. Mvc4+mysql make a simple paging component (partial view)

Source: Internet
Author: User
Tags mysql manual

  Before starting the MySQL paging functional component, it is necessary to have a certain reusability. First in the project in the Views folder right click New Name _paginationcomponent.cshtml, here HTML and CSS I use the bootstrap paging component, this can refer to http://v3.bootcss.com/components/.

The resulting project effect is presented first:

There is a need to know beforehand, is the MySQL paging query and the MSSQL paging query implementation difference is that MySQL supports the limit statement, the limit format is limit pageindex*pagesize,pagesize,pageindex--to the number of pages, pagesize--contains the amount of data for the page. The MySQL manual can be queried for specific usage of limit. Then you need to pre-define the Pagesize,pageindex,pagecount (total number of pages) of three quantities. The default pageSize is 30, which is: int pageSize = 30.

First of all to achieve PageCount: the idea is that all the data set to be fetched out of the database, according to pagesize = 30 to get the total number of pages, here will encounter a multiple of 30 data set is the problem, the solution is:

Mysqlcommand comm_1 =NewMysqlcommand (Sqlsearch, connection); Mysqldataadapter da_1=NewMysqldataadapter (Sqlsearch, connection); Da_1.selectcommand= Comm_1;//Initialize the Da.fill commandDataSet set_1 =NewDataSet ();            Da_1.fill (set_1); DataTable dt_1= set_1.tables[0];//use DataTable to load multiple table data and get the first table insideCount = (Double) (Dt_1.Rows.Count)/ -;//Total Pages Paged            if(Count > (int) (Dt_1.Rows.Count)/ -) {PageCount= (Dt_1.Rows.Count)/ -+1; }            Else if(Count = = (Dt_1.Rows.Count)/ -) {PageCount= (Dt_1.Rows.Count)/ -; }            Else if(Count < (int) (Dt_1.Rows.Count)/ -) {PageCount=1; }

Here to judge, more than 30 of the PageCount are added 1, less than 30 of the PageCount is 1.

The next thing to achieve is to use the pageindex page to get the corresponding data set, the idea is to use the Limit statement features:

  PublicList<model> Getdormitorybottlerecyclelistpagination (intPageIndex,intpageSize)            {get_connection (); List<Model> list =NULL;stringSqlpaginatiosearch ="SELECT * FROM table ORDER by File_1 ASC LIMIT"+ (PageIndex-1) * -+","+ PageSize +""; //fill in the full page of the dataMysqlcommand comm_2 =NewMysqlcommand (Sqlpaginatiosearch, connection); Mysqldataadapter da_2=NewMysqldataadapter (Sqlpaginatiosearch, connection); Da_2.selectcommand= Comm_2;//Initialize the Da.fill commandDataSet set_2 =NewDataSet ();            Da_2.fill (set_2); DataTable dt_2= set_2.tables[0];//use DataTable to load multiple table data and get the first table inside            if(Dt_2! =NULL&& Dt_2.Rows.Count >0) {List=NewList<model>(); foreach(DataRow rowinchdt_2.rows) {Model entity=SqlHelper.                    CreateItem (row); List.                ADD (entity);            }} close_connection (); returnlist; }
String sqlpaginatiosearch = "SELECT * FROM Table ORDER by File_1 ASC LIMIT" + (pageIndex-1) * + "," + PageSize + "; Fill in the full page of the data
This is the core SQL statement, pageIndex the number of incoming pages, starting at (PAGEINDEX-1) * 30 to take pagesize amount of data.

Implementation in the Controller's action is also key:

 PublicActionResult Dormitorybottlerecyclesort (intID =1)        {            intPageIndex =id;//Transfer of pagesintPageSize = -; intPageCount =0; List<BottleRecycleModel>list_1;list_1= Pbsaccess.getdormitorybottlerecyclelistpagination (PageIndex, PageSize, PageCount);//get a list of pagesViewbag.listcount =List_1.count; Bottlerecyclelist viewbottlerecyclelist=Newbottlerecyclelist (); Viewbottlerecyclelist.bottlerecyclelist=NewList<bottlerecyclemodel> ();//to instantiate an object, it is quite important
Here is the function code for displaying the paging data if(List_1! =NULL) { foreach(varIteminchlist_1) {Bottlerecyclemodel viewbottlerecycle=NewBottlerecyclemodel (); Viewbottlerecycle.id=item. Id; Viewbottlerecycle.dormitorynumber=item. Dormitorynumber; Viewbottlerecycle.smallbottlenumber=item. Smallbottlenumber; Viewbottlerecycle.bigbottlenumber=item. Bigbottlenumber; Viewbottlerecycle.totalbottlenumber=item. Totalbottlenumber; Viewbottlerecycle.publishtime=item. Publishtime; VIEWBOTTLERECYCLELIST.BOTTLERECYCLELIST.ADD (viewbottlerecycle); } viewbag.dormitorybottlerecyclesort=viewbottlerecyclelist.bottlerecyclelist; } Else{Viewbag.dormitorybottlerecyclesort=NULL; }
Viewbag.pagecount=Pbsaccess.getdormitorybottlepaginationpagecount (); Viewbag.pageindex=PageIndex; returnView (VIEWBAG); }

Here the viewbag is used to transmit the value, and here the Getdormitorybottlepaginationpagecount () is the method above PageCount. This is the method behind the scenes.

Now let's talk about how these values are used in _paginationcomponent.cshtml.

@{    stringControllers = viewcontext.routedata.values["Controller"].    ToString (); stringActions = viewcontext.routedata.values["Action"]. ToString ();}<li><a href="#">&laquo;</a></li>@for (inti =1; I < @ViewBag. PageCount +1; i++){    <li><a href="/@Controllers/@Actions/@i">@i</a></li>}<li><a href="#">&raquo;</a></li>

For component reuse, the viewcontext.routedata.values["controller"is used. ToString () method, so that when referencing components on other pages, you can easily migrate past such as:<a href="/@Controllers/@Actions/@i ">@i</a> Of course, the utility for loop is to show more pages, such as 1, 2, 3, 4, and so on, where my data is relatively small, so only one page is displayed, and of course there are other features such as omitting too many pages for ellipses and the current page to disable, etc. Need JS other code to implement, here only to implement a simple paging component function.

The code on this page is not the source of all parts of the page, here only to provide my personal solution when the idea, if there are errors, please correct me, must humbly consult.

//

ASP. Mvc4+mysql make a simple paging component (partial view)

Related Article

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.