PagedList分頁在MVC中的使用

1.在DAL層中

        /// <summary>
        /// 獲取新聞信息
        /// </summary>
        /// <param name="pageIndex">頁碼</param>
        /// <param name="pageSize">每頁數量</param>
        /// <param name="totalCount">總條數</param>
        /// <returns></returns>
        public List<News> GetNewsPage(int pageIndex,int pageSize,out int totalCount)
        {
            using (HotelDBCountext countext = new HotelDBCountext())
            {
                List<News> newslist = countext.News.ToList();//獲取總數
                totalCount = newslist.Count;
                foreach (var item in newslist)
                {
                    item.NewsCategory = countext.NewsCategory.Where(a=>a.CategoryId==item.CategoryId).FirstOrDefault();//新聞類表中ID與新聞表中新聞類ID對應
                }
                //倒序排序跳過每頁條數*頁碼-1留下每頁條數
                return newslist.OrderByDescending(item=>item.NewsId).Skip(pageSize*(pageIndex-1)).Take(pageSize).ToList();
            }
        }

2.在BLL層中

/// <summary>
        /// 獲取新聞信息
        /// </summary>
        /// <param name="pageIndex">頁碼</param>
        /// <param name="pageSize">每頁數量</param>
        /// <param name="totalCount">總條數</param>
        /// <returns></returns>
        public List<News> GetNewsPage(int pageIndex, int pageSize, out int totalCount)
        {
            return server.GetNewsPage(pageIndex,pageSize,out totalCount);
        }

3.在控制器中

		[HttpGet]
        [Authorize]
        //新聞管理
        public ActionResult NewManaer(int?id=1)
        {
            int totalCount = 0;//總條數 默認爲0
            int pageIndex = id??1;//頁碼
            int pageSIze = 5;//每頁條數
            List<News> list = new HotelNewsManager().GetNewsPage(pageIndex,pageSIze,out totalCount);
            PagedList<News> source = list.AsQueryable().ToPagedList(pageIndex,pageSIze);//數據源集合
            source.TotalItemCount = totalCount;//獲取或設置要分頁的記錄總數 
            source.CurrentPageIndex = id ?? 1;//獲取或設置當前顯示頁的索引
            ViewBag.DataSource = source;
            return View();
        }

4.在視圖界面

@using HotelModel;
@using Webdiyer.WebControls.Mvc;
@{
    ViewBag.Title = "新聞管理";
    Layout = "~/Views/Shatred/_LayoutManager.cshtml";
}
<link href="~/CSSmanager/NewsManage.css" rel="stylesheet" />
<link href="~/Style/Styles.css" rel="stylesheet" />
<script src="~/Scripts/jquery-3.5.1.min.js"></script>
<div id="content_div">
    <table border="1" cellspacing="0" cellpadding="0">
        <tr>
            <th>發佈時間</th>
            <th>新聞標題</th>
            <th>新聞分類</th>
            <th>操作</th>
        </tr>
        @{ 
            if (ViewBag.DataSource!=null&&((PagedList<News>)ViewBag.DataSource).Count>0)
            {
                foreach (var item in (PagedList<News>)ViewBag.DataSource)
                {
                    <tr>
                        <td>@HZJDHotel.Models.ReturnTypeCommon.ToShotrDate(item.PublishTime) </td>
                        <td><a href="#">@item.NewsTitle</a> </td>
                        <td>@item.NewsCategory.CategoryName</td>
                        <td class="btnTd">
                            @*<a href="#" οnclick="return confirm("確定要刪除該新聞?")">刪除</a>*@
                            @Html.ActionLink("修改", "UpdataNews", new { NewsId = item.NewsId,categoryId=item.CategoryId }, new { style = "color:black" })
                            @Html.ActionLink("刪除", "DeleteNews", new { NewsId = item.NewsId }, new { style = "color:black", onclick = "return confirm('確定要刪除該新聞?')" })
                        </td>
                    </tr>
                }
            }
        }
    </table>
    <!---插件樣式->
    <div style="margin-top:20px;" class="mvc_pager">
        @Html.Pager((PagedList<News>)ViewBag.DataSource, new PagerOptions()
        {
            PageIndexParameterName = "id",
            FirstPageText = "首頁",
            LastPageText = "尾頁",
            PrevPageText = "上一頁",
            NextPageText = "下一頁"
        })
        <div class="thispagethis">
            @Html.Raw("共:")@(((PagedList<News>)ViewBag.DataSource).TotalPageCount)@Html.Raw("頁")
            @(((PagedList<News>)ViewBag.DataSource).CurrentPageIndex)
            @Html.Raw("/")
            @(((PagedList<News>)ViewBag.DataSource).TotalPageCount)
            @Html.Raw("頁")
        </div>
    </div>
</div>

由於數據庫中只有六條數據所以只展現六條新聞

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章