zoukankan      html  css  js  c++  java
  • MVC学习(四)几种分页的实现(1)

       这里,我使用的是Code-First,MVC3。

      我们在数据库里建一个表MyTestPages,只有一个整型字段Id。
     
     在写一个Model类MyTestPages,代码如下
      public class MyTestPages
      {
        [Key]
        public int Id { get; set; }
      }
     
     建好表后,需要往里面插入一定量的数据,建议最好10万条以上,效果明显。
      首先看一下运行效果如下图所示。

      

      然后在HomeController里建一个名为Archtive的Action以及对于视图Archtive.cshtml。

      前台代码如下:

    @model IEnumerable<Models.MyTestPages>
    
        <script type="text/javascript">
            function GoPage(flag) {
            window.open("/home/Archtive/"+flag+"/"+@ViewBag.PIndex, "_self");
        }
        </script>
    
        <table border="1" cellpadding="0" cellspacing="0" height="200px" width="300px" bordercolor="blue">
            <tr>
               
                <th height="30px">
                    序号
                </th>
            </tr>
            @foreach(var item in Model)
            {
                <tr>
                    <td height="30px" align="center">
                        @item.Id
                    </td>
                </tr>
            }
           
        </table>
        <table border="0" cellpadding="0" cellspacing="0" width="300px">
             <tr align="center">
                <td style="height: 16px">
                    <input type="button" value="首页" name="First" id="First" onclick="GoPage('First')" />
                    <input type="button" value="上一页" name="Pre" id="Pre" onclick="GoPage('Pre')" />
                    <input type="button" value="下一页" name="Next" id="Next" onclick="GoPage('Next')" />
                    <input type="button" value="最后一页" name="Last" id="Last" onclick="GoPage('Last')" />
                </td>
            </tr>
        </table>
    前台代码

      在用户点击分页按钮时,调用了Js GoPage()函数

      window.open("/home/Archtive/"+flag+"/"+@ViewBag.PIndex, "_self");

      向Action传入了两个参数,MVC默认是只能传入一个参数的,因此,这里在添加了一个路由,代码如下(注意参数名称):

     routes.MapRoute("Default1",
                     "{controller}/{action}/{GoFlag}/{PageIndex}",
                     new { controller = "", action = "" },
                     new { });

      Controller代码如下:

       public ActionResult Archtive(string GoFlag, string PageIndex)
            {
                
                int PageSize = 5;
                int TotalCount = LzsDB.MyTestPages.Count();//获得此数据表中数据记录数
                double PageCount = Math.Ceiling((double)TotalCount / (double)PageSize);//获得总页数
                int NowPageIndex = 1;
                if (!string.IsNullOrEmpty(PageIndex))
                {
                    int ErrorPageIndex = 1;
                    if (!Int32.TryParse(PageIndex, out ErrorPageIndex))//如果不能转换成整数,则默认当前页码为1
                    {
                        PageIndex = "1";
                    }
    
                    NowPageIndex = Convert.ToInt32(PageIndex);//
                }
                GoFlag = string.IsNullOrEmpty(GoFlag) ? "First" : GoFlag;
                switch (GoFlag)
                {
                    case "First":
                        ViewBag.PIndex = 1;
                        NowPageIndex = 1;
                        break;
                    case "Pre":
                        if (Convert.ToInt32(PageIndex) - 1 <= 0)
                        {
                            ViewBag.PIndex = 1;
                            NowPageIndex = 1;
                        }
                        else
                        {
                            ViewBag.PIndex = Convert.ToInt32(PageIndex) - 1;
                            NowPageIndex = Convert.ToInt32(PageIndex) - 1;
                        }
                        break;
                    case "Next":
                        if (Convert.ToInt32(PageCount) - Convert.ToInt32(PageIndex) <= 0)
                //如果当前页是第最后页 则下一页没有后一页
                        {
                            ViewBag.PIndex = PageCount;
                            NowPageIndex = Convert.ToInt32(PageCount);
                        }
                        else
                        {
                            ViewBag.PIndex = Convert.ToInt32(PageIndex) + 1;
                            NowPageIndex = Convert.ToInt32(PageIndex) + 1;
                        }
                        break;
                    case "Last":
                        ViewBag.PIndex = PageCount;
                        NowPageIndex = Convert.ToInt32(PageCount);
                        break;
                }
    
                string LastPageSize = (PageSize * (NowPageIndex - 1)).ToString();
    
                string findSql = "select top " + PageSize + " * from MyTestPages "
                   + "where Id not in( select top " + LastPageSize + " Id from MyTestPages order by Id) order by Id";
    
                var TestPageModels = LzsDB.Database.SqlQuery<MyTestPages>(findSql);
                return View(TestPageModels.ToList());
            }    
    Controller代码

      这里对Linq to sql不太熟悉,因此,就使用了最原始的Sql分页语句获得数据。

  • 相关阅读:
    C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等
    百度编辑器(UEditor)自定义工具栏
    ASP.NET使用百度编辑器(UEditor)使用方法
    C#设计模式--桥接模式
    C#设计模式--外观模式
    Entity Framework6测试使用
    Entity Framework6的在线下载安装
    C#设计模式--代理模式
    php 回调函数结合闭包(匿名函数)的使用示例
    函数的可变参数实例
  • 原文地址:https://www.cnblogs.com/xianrongbin/p/3460880.html
Copyright © 2011-2022 走看看