ASP.net MVC分页实例

第一步:写Controllers里面的方法
        public ActionResult NewsList(int pgindex = 1)//默认索引为1
        {
            int counts = 0;//总的记录数
            int pageCount = 0;//总的页数

           //调用查询数据方法,下面会写出来
            ViewBag.newsList = new MvcHtmlString(NewsListMore("主题,大类,小类", "信息化,新闻", "", 1, pgindex, 20, "NewsList", out pageCount, out  counts, 14));

            //分页方法,直接在Views中生成分页标签,,,,,,,
            ViewBag.pagehtml = GetPageNum("/News/NewsList/", pgindex, 20, counts);
            return View();
        }

 

   #region 更多显示+NewsListMore()
        ///


        /// JoinComListPage
        ///

        /// 查询字段
        /// 条件
        ///
        /// 排列形式
        /// 索引
        /// 每页条数
        /// Class
        /// 页数
        /// 总记录数
        ///
        public string NewsListMore(string getFiles, string SqlWhere, string orderFile, int ordefType, int pageIndex, int pageSize, string CssClass, out int pageCount, out int counts, int WordNum)
        {
            string html = "";
            pageCount = 0; counts = 0;
            classDBOP db = new classDBOP();
            string strWhere = "and 1=1 ";
            string files = "ID";//getFiles 赋值
            if (getFiles.Trim().Length == 0)
            {
                return "";
            }
            else
            {
                if (getFiles.IndexOf("主题") > -1)
                    files += ",Title";
                if (getFiles.IndexOf("大类") > -1)
                    files += ",BigClass";
                if (getFiles.IndexOf("小类") > -1)
                    files += ",SmallClass";
            }
            if (SqlWhere.Trim().Length == 0)
            {
                return "";
            }
            else  
            {
                if (SqlWhere.IndexOf("信息化") > -1)
                    strWhere += "and BigClass='信息化'";
                if (SqlWhere.IndexOf("新闻") > -1)
                    strWhere += "and SmallClass='新闻'";
            }
            switch (orderFile)
            {
                case "名称":
                    orderFile = "Title";
                    break;
                default:
                    orderFile = "id";
                    break;
            }
            if (orderFile.Trim().Length == 0)
            {
                orderFile = "id";
            }
            if (CssClass.Trim().Length == 0)
            {
                CssClass = "NewsList";
            }

        ///


        /// 查询,(直接返回dt)重载
        ///

        /// 表名
        /// 查询sql
        /// 要提取的字段
        /// 主键字段
        /// /// 排序字段
        /// 排序方式,0,1
        /// 条数
        /// 页数
        /// distinct:0/1
        /// 总记录数
        /// 总页数
        /// 安全级别:1-字符+攻击,2-字符(公开新增加信息到库时必用),3-攻击(查询,新加都可能用到,前台查询时必用),4-无
        /// DT
       /// public DataTable Search_DT(string TableName, string SearchSQL, string GetFilds, string OrderFild, string PrimaryID, int OrderType,

///int SearchPageSize, int SearchPageIndex, string Dist, out int PageCount, out int Counts, int SafetySure)
     ///   {
            DataTable dt = db.Search_DT("CTUInfo", strWhere, files, orderFile, "ID", ordefType, pageSize, pageIndex, "", out pageCount, out counts, 4);
            string strTitle = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                html += "

";
                html += "";
                html += "主题";
                html += "大类";
                html += "小类";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strTitle = dt.Rows[i]["Title"].ToString();
                    if (WordNum > 0 && strTitle.Length > WordNum)
                    {
                        strTitle = strTitle.Substring(0, WordNum);
                    }
                    html += "
";
                    html += "" + dt.Rows[i]["BigClass"] + "";
                    html += "" + dt.Rows[i]["SmallClass"] + "
";
                }
                html += "
" + strTitle + "
";
                html += "
";
            }
            return html;
        }
        #endregion

 

       #region 分页
        public static string GetPageNum(string url, int page, int pagesize, int total)
        {
            int allpage = 0;
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string pagestr = "";
            /* int total = 100;*/
            if (page < 1) { page = 1; }
            /*计算总页数*/
            if (pagesize != 0)
            {
                allpage = (total / pagesize);
                allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
                allpage = (allpage == 0 ? 1 : allpage);
            }
            if (page > allpage)
            {
                page = allpage;
            }
            next = page + 1;
            pre = page - 1;
            startcount = (page + 5) > allpage ? allpage - 9 : page - 5;/*中间页起始序号*/
            /*中间页终止序号*/
            endcount = page < 5 ? 10 : page + 4;

            if (startcount < 1)
            {
                startcount = 1;
            } /*为了避免输出的时候产生负数,设置如果小于1就从序号1开始*/
            if (allpage < endcount)
            {
                endcount = allpage;
            } /*页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内*/
            pagestr += "共" + allpage + "页," + total + "条";
            pagestr += page > 1 ? "首页上一页" : "";
            /*中间页处理,这个增加时间复杂度,减小空间复杂度 #ff0000:red*/
            for (int i = startcount; i <= endcount; i++)
            {
                pagestr += page == i ? "" + i + "" : "" + i + "";
            }
            pagestr += page != allpage ? "下一页末页" : "";
            pagestr += "";

            pagestr += "跳转";//InfoListPost\

            return pagestr;
        }
        #endregion

 

 

第二步:View页面

 

 


    @ViewBag.newsList
    @{MvcHtmlString pagehtml = new MvcHtmlString(@ViewBag.pagehtml); }
 
@pagehtml

 

 

第三部:数据库分页存储过程编写,这个在我的SQL中

你可能感兴趣的:(MVC)