asp.net mvc 分页 CPager(秒杀杨涛MvcPager)之封装之后

基于路由的分页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CPager.Models;
using System.Data.Entity;

namespace CPager.Controllers
{
    public class HomeController : Controller
    {
        NiceMusicDBContext db = new NiceMusicDBContext();
        public ActionResult Index(int id = 1)
        {
            ViewBag_Table_PageControll("MP3", id, 10, "tb1 position2", "parent_div position2", "Home", "Index", "fpnl", "co", "disabled", "active");
            //return View(List_ViewBag<MP3>("MP3",id,20));
            return View();
        }
        public List<T> List_ViewBag<T>(string TableName, int PageIndex, int PageSize)
        {
            string sql1 = $"select count(*) from [{TableName}]";
            int DataCount = db.Database.SqlQuery<int>(sql1).ToList()[0];
            int PageCount = DataCount % PageSize == 0 ? DataCount / PageSize : DataCount / PageSize + 1;
            int min = PageSize * (PageIndex - 1);
            string sql2 = $"select top {PageSize} * from [{TableName}] where (ID not in(select top {min} ID from [{TableName}]))";
            List<T> l = db.Database.SqlQuery<T>(sql2).ToList();
            if (PageCount <= PageSize)
            {
                ViewBag.Flag = 0;
            }
            else
            {
                if (PageIndex <= 6)
                {
                    ViewBag.Flag = 1;
                }
                else
                {
                    if (PageIndex < PageCount - 4)
                    {
                        ViewBag.Flag = 2;
                    }
                    else
                    {
                        ViewBag.Flag = 3;
                    }
                }
            }
            ViewBag.PageIndex = PageIndex;
            ViewBag.PageCount = PageCount;
            return l;
        }
        public void ViewBag_Table_PageControll(string DBTableName, int PageIndex, int PageSize, string Table_ClassName, string ParentDiv_ClassName, string ControllerName, string ActionName, string FPNL_ClassName, string General_ClassName, string Disabled_ClassName, string Active_ClassName)
        {
            ViewBag.Table_String = ViewBag.Page_String = "";
            string sql1 = $"select count(*) from [{DBTableName}]";
            int DataCount = db.Database.SqlQuery<int>(sql1).ToList()[0];
            int PageCount = DataCount % PageSize == 0 ? DataCount / PageSize : DataCount / PageSize + 1;
            int min = PageSize * (PageIndex - 1);
            string sql2 = $"select top {PageSize} * from [{DBTableName}] where (ID not in(select top {min} ID from [{DBTableName}]))";
            List<MP3> l = db.Database.SqlQuery<MP3>(sql2).ToList();

            //构造表格字符串开始
            ViewBag.Table_String += $"<table class=\"{Table_ClassName}\">";
            ViewBag.Table_String += $"<tr>";
            List<string> Table_HeaderName = new List<string>()
            {
                "编号","歌手","性别","分类","音乐名称","片段标识","片段歌词","时间(s)"
            };
            for (int i = 0; i < Table_HeaderName.Count; i++)
            {
                ViewBag.Table_String += $"<td>{Table_HeaderName[i]}</td>";
            }
            ViewBag.Table_String += $"</tr>";
            for (int i = 0; i < l.Count; i++)
            {
                ViewBag.Table_String += $"<tr>";
                ViewBag.Table_String += $"<td>{l[i].Id}</td>";
                ViewBag.Table_String += $"<td>{l[i].Singer_Name}</td>";
                ViewBag.Table_String += $"<td>{l[i].Singer_Sex}</td>";
                ViewBag.Table_String += $"<td>{l[i].Singer_Type}</td>";
                ViewBag.Table_String += $"<td>{l[i].Song_Name}</td>";
                ViewBag.Table_String += $"<td>{l[i].Song_Id}</td>";
                ViewBag.Table_String += $"<td><a href=\"#\">查看</a></td>";
                ViewBag.Table_String += $"<td>{l[i].Time}</td>";
                ViewBag.Table_String += $"</tr>";
            }
            ViewBag.Table_String += $"</table>";
            //构造表格字符串结束

            //构造分页控件字符串开始
            ViewBag.Page_String += $"<div class=\"{ParentDiv_ClassName}\">";
            if (PageCount <= PageSize)
            {
                if (PageIndex == 1)
                {
                    ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">首页</a>";
                    ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">上页</a>";
                }
                else
                {
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/1\" class=\"{FPNL_ClassName}\">首页</a>";
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex - 1}\" class=\"{FPNL_ClassName}\">上页</a>";
                }
                for (int i = 1; i <= PageCount; i++)
                {
                    if (i == PageIndex)
                    {
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName} {Active_ClassName}\">{i}</a>";
                        continue;
                    }
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName}\">@i</a>";
                }
                if (PageIndex == PageCount)
                {
                    ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">下页</a>";
                    ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">尾页</a>";
                }
                else
                {
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex + 1}\" class=\"{FPNL_ClassName}\">下页</a>";
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageCount}\" class=\"{FPNL_ClassName}\">尾页</a>";
                }
            }
            else
            {
                if (PageIndex <= 6)
                {
                    if (PageIndex == 1)
                    {
                        ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">首页</a>";
                        ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">上页</a>";
                    }
                    else
                    {
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/1\" class=\"{FPNL_ClassName}\">首页</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex - 1}\" class=\"{FPNL_ClassName}\">上页</a>";
                    }
                    for (int i = 1; i <= 10; i++)
                    {
                        if (i == PageIndex)
                        {
                            ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName} {Active_ClassName}\">{i}</a>";
                            continue;
                        }
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName}\">{i}</a>";
                    }
                    ViewBag.Page_String += $"<a href=\"/{ ControllerName}/{ ActionName}/11\" class=\"{General_ClassName}\">...</a>";
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex + 1}\" class=\"{FPNL_ClassName}\">下页</a>";
                    ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageCount}\" class=\"{FPNL_ClassName}\">尾页</a>";
                }
                else
                {
                    if (PageIndex < PageCount - 4)
                    {
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/1\" class=\"{FPNL_ClassName}\">首页</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex - 1}\" class=\"{FPNL_ClassName}\">上页</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex - 6}\" class=\"{General_ClassName}\">...</a>";
                        for (int i = PageIndex - 5; i <= PageIndex + 4; i++)
                        {
                            if (i == PageIndex)
                            {
                                ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName} {Active_ClassName}\">{i}</a>";
                                continue;
                            }
                            ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName}\">{i}</a>";
                        }
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex + 5}\" class=\"{General_ClassName}\">...</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex + 1}\" class=\"{FPNL_ClassName}\">下页</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageCount}\" class=\"{FPNL_ClassName}\">尾页</a>";
                    }
                    else
                    {
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/1\" class=\"{FPNL_ClassName}\">首页</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex - 1}\" class=\"{FPNL_ClassName}\">上页</a>";
                        ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageCount - 10}\" class=\"{General_ClassName}\">...</a>";
                        for (int i = PageCount - 9; i <= PageCount; i++)
                        {
                            if (i == PageIndex)
                            {
                                ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName} {Active_ClassName}\">{i}</a>";
                                continue;
                            }
                            ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{i}\" class=\"{General_ClassName}\">{i}</a>";
                        }
                        if (PageIndex == PageCount)
                        {
                            ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">下页</a>";
                            ViewBag.Page_String += $"<a class=\"{FPNL_ClassName} {Disabled_ClassName}\">尾页</a>";
                        }
                        else
                        {
                            ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageIndex + 1}\" class=\"{FPNL_ClassName}\">下页</a>";
                            ViewBag.Page_String += $"<a href=\"/{ControllerName}/{ActionName}/{PageCount}\" class=\"{FPNL_ClassName}\">尾页</a>";
                        }
                    }
                }
            }
            ViewBag.Page_String += $"</div>";
            //构造分页控件字符串结束
        }
    }
}
@model IEnumerable<CPager.Models.MP3>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CPager</title>
    <link href="~/CFrame.css" rel="stylesheet" />
    <script src="~/CFrame.js"></script>
</head>
<body>
    @Html.Raw(ViewBag.Table_String)
    @Html.Raw(ViewBag.Page_String)
    
    @*<table class="tb1 position2">
        <tr>
            <td>编号</td>
            <td>歌手名字</td>
            <td>歌手性别</td>
            <td>歌手分类</td>
            <td>音乐名称</td>
            <td>片段标识</td>
            <td>片段歌词</td>
            <td>时间(s)</td>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Id</td>
                <td>@item.Singer_Name</td>
                <td>@item.Singer_Sex</td>
                <td>@item.Singer_Type</td>
                <td>@item.Song_Name</td>
                <td>@item.Song_Id</td>
                <td><a href="#">查看</a></td>
                <td>@item.Time</td>
            </tr>
        }
    </table>
    <div class="parent_div position2">
        @if (ViewBag.Flag == 0)
        {
            int p = ViewBag.PageIndex - 1;
            string p_url = "/Home/Index/" + p;
            string n_url = "/Home/Index/" + (p + 2);
            string l_url = "/Home/Index/" + ViewBag.PageCount;
            if (ViewBag.PageIndex == 1)
            {
                <a class="fpnl disabled">首页</a>
                <a class="fpnl disabled">上页</a>
            }
            else
            {
                <a href="/Home/Index/1" class="fpnl">首页</a>
                <a href=@p_url class="fpnl">上页</a>
            }
            for (int i = 1; i <= ViewBag.PageCount; i++)
            {
                string g_url = "/Home/Index/" + i;
                if (i == p + 1)
                {
                    <a href=@g_url class="co active">@i</a>
                    continue;
                }
                <a href=@g_url class="co">@i</a>
            }
            if (ViewBag.PageIndex == ViewBag.PageCount)
            {
                <a class="fpnl disabled">下页</a>
                <a class="fpnl disabled">尾页</a>
            }
            else
            {
                <a href=@n_url class="fpnl">下页</a>
                <a href=@l_url class="fpnl">尾页</a>
            }
        }
        else if (ViewBag.Flag == 1)
        {
            int p = ViewBag.PageIndex - 1;
            string p_url = "/Home/Index/" + p;
            string n_url = "/Home/Index/" + (p + 2);
            string l_url = "/Home/Index/" + ViewBag.PageCount;
            if (ViewBag.PageIndex == 1)
            {
                <a class="fpnl disabled">首页</a>
                <a class="fpnl disabled">上页</a>
            }
            else
            {
                <a href="/Home/Index/1" class="fpnl">首页</a>
                <a href=@p_url class="fpnl">上页</a>
            }
            for (int i = 1; i <= 10; i++)
            {
                string g_url = "/Home/Index/" + i;
                if (i == p + 1)
                {
                    <a href=@g_url class="co active">@i</a>
                    continue;
                }
                <a href=@g_url class="co">@i</a>
            }
            <a href="/Home/Index/11" class="co">...</a>
            <a href=@n_url class="fpnl">下页</a>
            <a href=@l_url class="fpnl">尾页</a>
        }
        else if (ViewBag.Flag == 2)
        {
            int p = ViewBag.PageIndex - 1;
            string p_url = "/Home/Index/" + p;
            string nonel_url = "/Home/Index/" + (ViewBag.PageIndex - 6);
            string noner_url = "/Home/Index/" + (ViewBag.PageIndex + 5);
            string n_url = "/Home/Index/" + (p + 2);
            string l_url = "/Home/Index/" + ViewBag.PageCount;
            <a href="/Home/Index/1" class="fpnl">首页</a>
            <a href=@p_url class="fpnl">上页</a>
            <a href=@nonel_url class="co">...</a>
            for (int i = p - 4; i <= p + 5; i++)
            {
                string g_url = "/Home/Index/" + i;
                if (i == p + 1)
                {
                    <a href=@g_url class="co active">@i</a>
                    continue;
                }
                <a href=@g_url class="co">@i</a>
            }
            <a href=@noner_url class="co">...</a>
            <a href=@n_url class="fpnl">下页</a>
            <a href=@l_url class="fpnl">尾页</a>
        }
        else if (ViewBag.Flag == 3)
        {
            int p = ViewBag.PageIndex - 1;
            string p_url = "/Home/Index/" + p;
            string nonel_url = "/Home/Index/" + (ViewBag.PageCount - 10);
            string n_url = "/Home/Index/" + (p + 2);
            string l_url = "/Home/Index/" + ViewBag.PageCount;
            <a href="/Home/Index/1" class="fpnl">首页</a>
            <a href=@p_url class="fpnl">上页</a>
            <a href=@nonel_url class="co">...</a>
            for (int i = ViewBag.PageCount - 9; i <= ViewBag.PageCount; i++)
            {
                string g_url = "/Home/Index/" + i;
                if (i == p + 1)
                {
                    <a href=@g_url class="co active">@i</a>
                    continue;
                }
                <a href=@g_url class="co">@i</a>
            }
            if (ViewBag.PageIndex == ViewBag.PageCount)
            {
                <a class="fpnl disabled">下页</a>
                <a class="fpnl disabled">尾页</a>
            }
            else
            {
                <a href=@n_url class="fpnl">下页</a>
                <a href=@l_url class="fpnl">尾页</a>
            }
        }
    </div>*@
</body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值