国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET MVC4 Razor模板簡易分頁效果

2024-07-10 12:46:39
字體:
供稿:網(wǎng)友

一、無數(shù)據(jù)提交

第一步,建立一個 Controller命名為PageIndex的空控制器,自定義一個方法如下:   

    public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)    {      //int count = db.Product.Count();      ViewBag.PageCount = pageCount;//從操作中獲取總數(shù)據(jù)頁數(shù)將傳入分頁視圖頁面      ViewBag.CurrentPage = currentPage;//從操作中獲取當(dāng)前頁數(shù)將傳入分頁視圖頁面      ViewBag.action = action;      ViewBag.controller = controller;      return PartialView();    }

傳入四個參數(shù): 

action:操作(要分頁的視圖的操作,默認(rèn)為Index);

controller:控制器;

currentPage:當(dāng)前頁數(shù);

pageCount:數(shù)據(jù)總頁數(shù)

第二步:添加視圖(PageIndex)

@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)    {      <span>您好,當(dāng)前沒有數(shù)據(jù)顯示!</span>    }    else    {      if (ViewBag.CurrentPage <= 10)    {    <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">    首頁</a>|</span>    }  else  {  <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">    首頁</a>  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)">    ...</a> </span>   }  for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)  {    if (i <= 0)    {      continue;    }    if (i > ViewBag.PageCount)    {      break;    }  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)">    第 @i 頁</a>|</span>  }  if (ViewBag.CurrentPage > 1)  {  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)">    上一頁</a>|</span>  }  if (ViewBag.PageCount > ViewBag.CurrentPage)  {  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)">    下一頁</a></span>  }  if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)  {    <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">    尾 頁</a>  }  else  {  <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)">    ...</a></span>  <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">    尾 頁</a>  }  <span style="padding-left: 20px">當(dāng)前頁數(shù): @ViewBag.CurrentPage | 共 @ViewBag.PageCount 頁  </span>    }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石屏县| 静宁县| 郴州市| 仪征市| 隆回县| 舟山市| 利辛县| 资溪县| 星子县| 浦江县| 瓦房店市| 长兴县| 任丘市| 斗六市| 名山县| 唐河县| 大洼县| 中方县| 资讯 | 敖汉旗| 田阳县| 高雄市| 中西区| 当涂县| 揭东县| 永嘉县| 陆丰市| 额济纳旗| 黄石市| 福海县| 鄱阳县| 遂川县| 余干县| 杭锦后旗| 象山县| 花莲市| 余庆县| 垦利县| 弥渡县| 体育| 马公市|