麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:31:22
字體:
來源:轉載
供稿:網友

一、無數據提交

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

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

傳入四個參數: 

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

controller:控制器;

currentPage:當前頁數;

pageCount:數據總頁數

第二步:添加視圖(PageIndex)

@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)    {      <span>您好,當前沒有數據顯示!</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">當前頁數: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 頁  </span>    }

第三步:操作的視圖的控制器修改

public ViewResult Index(int? pageIndex)    {      int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;       ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);      //這里的是take,按照每頁20個顯示      return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));    }

第四步:頁面調用(即最后一步)

 

復制代碼 代碼如下:
@Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

 

一般來說,數據都是變動的。 

二、有數據提交

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

    public ActionResult PageIndexKey(int currentPage, int pageCount)    {      ViewBag.PageCount = pageCount;//從操作中獲取總數據頁數將傳入分頁視圖頁面      ViewBag.CurrentPage = currentPage;//從操作中獲取當前頁數將傳入分頁視圖頁面      return PartialView();    }

第二步:建立分布視圖

 

 <script>  $(function () {    $("#pageingByForm a").click(function (event) {      $("#pageIndex").val($(this).attr("pageIndex"));      //$(this).parent("Form").submit();      document.getElementsByTagName("Form").item(0).submit();      event.preventDefault();    });  });</script>@Html.Hidden("pageIndex")<div id="pageingByForm">  @if (ViewBag.PageCount == null || ViewBag.PageCount == 0)  {    <span>當前沒有數據</span>  }  else  {    if (ViewBag.CurrentPage <= 10)    {    <span><a pageindex="1" href="#">首頁</a>|</span>    }    else    {    <span><a pageindex="1" href="#">首頁</a>|</span>    <span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span>    }    for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)    {      if (i <= 0)      {        continue;      }      if (i > ViewBag.PageCount)      {        break;      }    <span><a pageIndex="@i" href="#">第 @i 頁</a>|</span>    }    if (ViewBag.CurrentPage >1)    {    <span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">上一頁</a>|</span>    }    if (ViewBag.PageCount > ViewBag.CurrentPage)    {    <span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">下一頁</a></span>    }    if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)    {    }    else    {    <span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span>    <span><a pageIndex="@ViewBag.PageCount" href="#">尾 頁</a></span>    }    <span style="padding-left: 20px">當前頁數: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 頁    </span>  }</div>

第三步:修改操作視圖和控制器

public ViewResult Index(int? pageIndex ,string search)  {  int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;   ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);   return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));  }

視圖(頁面調用):
 @using (Html.BeginForm())

根據性別得到查詢結果 

性別: @Html.TextBox("sex")

<input type="submit" value="查詢" />  

@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
 

Example: 

    //數據,一個list的集合     List<string> s = new List<string>();       s.Add("張軍");       ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0);       return View(s.Skip((pageInd - 1) * 20).Take(20));     @Html.Action("PageIndex", "PageIndex",     new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美日韩在线播放 | 日本在线视 | 91久久极品少妇韩国 | 深夜视频在线观看 | 精品国产精品久久 | av在线等| 亚洲日本欧美 | 国产精品美女久久久免费 | 一级黄色性感片 | 欧美一级美国一级 | 中午字幕无线码一区2020 | 欧美重口另类videos人妖 | 欧美日韩大片在线观看 | 国产精品视频成人 | 久草干| 欧美性激情视频 | 欧美一区二区黄色 | 欧美女优一区 | 麻豆19禁国产青草精品 | 黄网站免费在线看 | 在线亚洲免费视频 | 久久精品亚洲欧美日韩精品中文字幕 | 国产亚洲精品综合一区 | aa国产视频一区二区 | 国产成人精品一区二区视频免费 | 久久国产夫妻视频 | www.国产一区.com | javhdfreejaⅴhd | 日本在线不卡免费 | 中日韩乱码一二新区 | 毛片免费在线播放 | 午夜影视一区二区 | 免费中文视频 | 在线看一区二区三区 | 免费网址黄 | 欧美激情天堂 | 久久99精品久久久久久青青日本 | 爱视频福利| 国产美女视频一区二区三区 | 日韩欧美动作影片 | 国内精品一级毛片免费看 |