Asp.net MVC3表格共用分頁功能

在創建的mvc3項目中,在Razor(CSHTML)視圖引擎下,數據會在表格中自動的生成,但分頁沒有好的控件實現,這裏咱們開發了設計了一個分頁的模板,適合於沒有數據提交和有數據提交的分頁的分頁。mvc

第一:沒有有數據提交的分頁功能:           this

第一步,創建一個 Controller命名爲PageIndex的空控制器,自定義一個方法以下:            spa

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();
        }
傳入四個參數,分別爲:操做,控制器,當前頁數,數據總頁數,操做是指你要分頁的試圖的操做通常爲Index,控制器爲該試圖對應的控制器, 這四個都爲視圖傳遞數據的。        
第二步:在PageIndex控制器下的Index上添加視圖選擇分佈視圖,代碼以下:   
@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>
}  在操縱中(Index)的部分代碼:  public ViewResult Index(int? pageIndex) { 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)); } 這個意思是以二十也一頁做爲當前頁. 在你要分頁的頁面的下面加入調用: @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();
        }            第二步:在要分頁的頁面Index上添加視圖選擇分佈視圖,代碼以下 <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> 
在操縱中(Index)的部分代碼:
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 })}  其餘的和第一中相符  。
示例:
            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 = "Index", controller = "LogController", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })  
相關文章
相關標籤/搜索