溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

asp.netmvc4mysql制作簡(jiǎn)單分頁(yè)組件的示例分析

發(fā)布時(shí)間:2021-09-16 15:37:29 來(lái)源:億速云 閱讀:113 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家介紹asp.netmvc4mysql制作簡(jiǎn)單分頁(yè)組件的示例分析,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

在開(kāi)始做mysql分頁(yè)功能組件前,便設(shè)定的是要有一定可復(fù)用性。先在項(xiàng)目里Views文件夾下右鍵新建名為_(kāi)PaginationComponent.cshtml,這里html及css我采用的bootstrap分頁(yè)組件,這可以參考http://v3.bootcss.com/components/。

先將生成項(xiàng)目效果截圖呈上:

asp.netmvc4mysql制作簡(jiǎn)單分頁(yè)組件的示例分析

  這里有需要預(yù)先知道的,是mysql分頁(yè)查詢與mssql分頁(yè)查詢實(shí)現(xiàn)不同點(diǎn)在于,mysql支持limit語(yǔ)句,limit格式為 limit pageIndex*pageSize,pageSize,pageIndex--為頁(yè)數(shù),pageSize--為頁(yè)面包含數(shù)據(jù)量。limit具體用法可查詢mysql手冊(cè)。然后需要預(yù)先定義好pageSize,pageIndex,pageCount(分頁(yè)總數(shù))三個(gè)量。這里預(yù)設(shè)pageSize為30,即: int pageSize = 30。

  首先來(lái)實(shí)現(xiàn)獲取pageCount:思路是,先將要獲取的所有數(shù)據(jù)集從數(shù)據(jù)庫(kù)中取出,根據(jù)pageSize = 30得出總頁(yè)數(shù),這里會(huì)遇到數(shù)據(jù)集數(shù)量為30的倍數(shù)與否問(wèn)題,解決是:

 MySqlCommand comm_1 = new MySqlCommand(sqlSearch, connection);
   MySqlDataAdapter da_1 = new MySqlDataAdapter(sqlSearch, connection);
   da_1.SelectCommand = comm_1; //初始化da.fill的命令
   DataSet set_1 = new DataSet();
   da_1.Fill(set_1);
   DataTable dt_1 = set_1.Tables[0]; //使用datatable裝所得多張表數(shù)據(jù),并獲取里面的第一張表  
   Count = (double)(dt_1.Rows.Count) / 30;//分頁(yè)總頁(yè)數(shù)
   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。

接下來(lái)要實(shí)現(xiàn)的是用pageIndex傳頁(yè)數(shù)獲取對(duì)應(yīng)的數(shù)據(jù)集,思路是利用limit語(yǔ)句特性: 

 public List<Model> GetDormitoryBottleRecycleListPagination(int pageIndex, int pageSize)
  {
   Get_Connection();
   List<Model> list = null;string sqlPaginatioSearch = "SELECT * FROM table ORDER BY file_1 ASC LIMIT " + (pageIndex - 1) * 30 + "," + pageSize + "";      //填充分頁(yè)后的數(shù)據(jù)
   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裝所得多張表數(shù)據(jù),并獲取里面的第一張表     
   if (dt_2 != null && dt_2.Rows.Count > 0)
   {
    list = new List<Model>();
    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 + ""; //填充分頁(yè)后的數(shù)據(jù)
這是核心sql語(yǔ)句,通過(guò)pageIndex傳入頁(yè)面數(shù),從(pageIndex - 1) * 30處開(kāi)始取pageSize量的數(shù)據(jù)。
在控制器的action中實(shí)現(xiàn)也是關(guān)鍵:

public ActionResult DormitoryBottleRecycleSort(int id = 1)
  {
   int pageIndex = id;//傳遞分頁(yè)數(shù)
   int pageSize = 30;
   int pageCount = 0;   
   List<BottleRecycleModel> list_1;
   list_1 = pbsAccess.GetDormitoryBottleRecycleListPagination(pageIndex, pageSize, pageCount);//獲取分頁(yè)列表
   ViewBag.ListCount = list_1.Count;
   BottleRecycleList viewBottleRecycleList = new BottleRecycleList();
   viewBottleRecycleList.bottleRecycleList = new List<BottleRecycleModel>();//要實(shí)例化對(duì)象,相當(dāng)重要
        //這里是為顯示分頁(yè)數(shù)據(jù)的功能代碼
   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進(jìn)行傳值,這里的getDormitoryBottlePaginationPageCount()就是上面pageCount得出的方法。這是后臺(tái)的方法。

  現(xiàn)在說(shuō)一下_PaginationComponent.cshtml里該如何運(yùn)用這些值。

@{
 string Controllers = ViewContext.RouteData.Values["controller"].ToString();
 string Actions = ViewContext.RouteData.Values["Action"].ToString();
}
<li><a href="#">&laquo;</a></li>
@for (int i = 1; i < @ViewBag.pageCount + 1; i++)
{
 <li><a href="/@Controllers/@Actions/@i">@i</a></li>
}

<li><a href="#">&raquo;</a></li>

  為了進(jìn)行組件復(fù)用,采用ViewContext.RouteData.Values["controller"].ToString()方法,這樣,在其他頁(yè)面里引用組件時(shí),可以輕易移植過(guò)去如:<a href="/@Controllers/@Actions/@i">@i</a>,當(dāng)然,實(shí)用for循環(huán)是為了顯示出更多的頁(yè)數(shù),諸如1、2、3、4等等,這里我的數(shù)據(jù)比較少,因此只顯示一頁(yè),當(dāng)然還有其他功能諸如省略過(guò)多頁(yè)數(shù)為省略號(hào)和當(dāng)前頁(yè)禁用等等,需要js其他代碼來(lái)實(shí)現(xiàn),這里只為了實(shí)現(xiàn)一個(gè)簡(jiǎn)單的分頁(yè)組件功能。

關(guān)于asp.netmvc4mysql制作簡(jiǎn)單分頁(yè)組件的示例分析就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI