Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of simple paging components made by asp.netmvc4mysql

2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the asp.netmvc4mysql production of simple paging components of the example analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Before starting to do mysql paging function components, it is set to have a certain degree of reusability. First, right-click under the Views folder in the project to create a new name named _ PaginationComponent.cshtml, where html and css I use the bootstrap paging component, which can refer to http://v3.bootcss.com/components/.

First, submit the screenshot of the generated project effect:

What you need to know in advance is that the implementation of mysql paging query is different from mssql paging query in that mysql supports limit statements, limit format is limit pageIndex*pageSize,pageSize,pageIndex-- for the number of pages, and pageSize-- is the amount of data contained in the page. The specific usage of limit can be found in the mysql manual. Then you need to pre-define three quantities of pageSize,pageIndex,pageCount (total number of pages). Here the default pageSize is 30, that is, int pageSize = 30.

First of all, to obtain pageCount: the idea is that all the datasets to be acquired are taken out of the database first, and the total number of pages is obtained according to pageSize = 30. Here, you will encounter the problem of whether the number of datasets is a multiple of 30. The solution is:

MySqlCommand comm_1 = new MySqlCommand (sqlSearch, connection); MySqlDataAdapter da_1 = new MySqlDataAdapter (sqlSearch, connection); da_1.SelectCommand = comm_1; / / command to initialize da.fill DataSet set_1 = new DataSet (); da_1.Fill (set_1); DataTable dt_1 = set_1.Tables [0] / / use datatable to load the data of multiple tables, and get the first table Count = (double) (dt_1.Rows.Count) / 30 bump / total paged pages if (Count > (int) (dt_1.Rows.Count) / 30) {pageCount = (dt_1.Rows.Count) / 30 + 1 } else if (Count = = (dt_1.Rows.Count) / 30) {pageCount = (dt_1.Rows.Count) / 30;} else if (Count

< (int)(dt_1.Rows.Count) / 30) { pageCount = 1; } 这里用到判断,大于30的pageCount均往上加1,小于30的pageCount为1。 接下来要实现的是用pageIndex传页数获取对应的数据集,思路是利用limit语句特性: public List GetDormitoryBottleRecycleListPagination(int pageIndex, int pageSize) { Get_Connection(); List list = null;string sqlPaginatioSearch = "SELECT * FROM table ORDER BY file_1 ASC LIMIT " + (pageIndex - 1) * 30 + "," + pageSize + ""; //填充分页后的数据 MySqlCommand comm_2 = new MySqlCommand(sqlPaginatioSearch, connection); MySqlDataAdapter da_2 = new MySqlDataAdapter(sqlPaginatioSearch, connection); da_2.SelectCommand = comm_2; //初始化da.fill的命令 DataSet set_2 = new DataSet(); da_2.Fill(set_2); DataTable dt_2 = set_2.Tables[0]; //使用datatable装所得多张表数据,并获取里面的第一张表 if (dt_2 != null && dt_2.Rows.Count >

0) {list = new List (); foreach (DataRow row in dt_2.Rows) {Model entity = sqlhelper.CreateItem (row); list.Add (entity);}} Close_Connection (); return list;}

String sqlPaginatioSearch = "SELECT * FROM table ORDER BY file_1 ASC LIMIT" + (pageIndex-1) * 30 + "," + pageSize + ""; / / data after completing the full page

This is the core sql statement that passes in the number of pages through pageIndex, fetching the amount of pageSize data from (pageIndex-1) * 30.

Implementation in the action of the controller is also key:

Public ActionResult DormitoryBottleRecycleSort (int id = 1) {int pageIndex = id;// delivery pages int pageSize = 30; int pageCount = 0; List list_1; list_1 = pbsAccess.GetDormitoryBottleRecycleListPagination (pageIndex, pageSize, pageCount); / / get paging list ViewBag.ListCount = list_1.Count; BottleRecycleList viewBottleRecycleList = new BottleRecycleList (); viewBottleRecycleList.bottleRecycleList = new List () / / it is very important to instantiate the object / / here is the function code if (list_1! = null) {foreach (var item in list_1) {BottleRecycleModel viewBottleRecycle = new BottleRecycleModel (); 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; return View (ViewBag);}

ViewBag is used here to pass values, and the getDormitoryBottlePaginationPageCount () here is the method obtained by pageCount above. This is the backstage method.

Now let's talk about how to use these values in _ PaginationComponent.cshtml.

@ {string Controllers = ViewContext.RouteData.Values ["controller"] .ToString (); string Actions = ViewContext.RouteData.Values ["Action"] .ToString ();} «@ for (int I = 1; I < @ ViewBag.pageCount + 1; iTunes +) {@ I} »

For component reuse, use the ViewContext.RouteData.Values ["controller"] .ToString () method, so that when referencing components in other pages, you can easily port to the past such as: @ I, of course, the practical for loop is to show more pages, such as 1, 2, 3, 4, etc., where I have less data, so only one page is displayed Of course, there are other features, such as omitting too many pages for ellipsis and disabling the current page, and so on, which need to be implemented by other js code, just to achieve a simple paging component function.

Asp.netmvc4mysql on the production of simple paging components of the sample analysis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 210

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report