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>

由于数据库中只有六条数据所以只展现六条新闻

在这里插入图片描述
在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页