MVC学习(四)几种分页的实现(1)

时间:2023-03-10 01:13:50
MVC学习(四)几种分页的实现(1)

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

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

  MVC学习(四)几种分页的实现(1)

  然后在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="" cellpadding="" cellspacing="" 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="" cellpadding="" cellspacing="" 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 = ;
int TotalCount = LzsDB.MyTestPages.Count();//获得此数据表中数据记录数
double PageCount = Math.Ceiling((double)TotalCount / (double)PageSize);//获得总页数
int NowPageIndex = ;
if (!string.IsNullOrEmpty(PageIndex))
{
int ErrorPageIndex = ;
if (!Int32.TryParse(PageIndex, out ErrorPageIndex))//如果不能转换成整数,则默认当前页码为1
{
PageIndex = "";
} NowPageIndex = Convert.ToInt32(PageIndex);//
}
GoFlag = string.IsNullOrEmpty(GoFlag) ? "First" : GoFlag;
switch (GoFlag)
{
case "First":
ViewBag.PIndex = ;
NowPageIndex = ;
break;
case "Pre":
if (Convert.ToInt32(PageIndex) - <= )
{
ViewBag.PIndex = ;
NowPageIndex = ;
}
else
{
ViewBag.PIndex = Convert.ToInt32(PageIndex) - ;
NowPageIndex = Convert.ToInt32(PageIndex) - ;
}
break;
case "Next":
if (Convert.ToInt32(PageCount) - Convert.ToInt32(PageIndex) <= )
//如果当前页是第最后页 则下一页没有后一页
{
ViewBag.PIndex = PageCount;
NowPageIndex = Convert.ToInt32(PageCount);
}
else
{
ViewBag.PIndex = Convert.ToInt32(PageIndex) + ;
NowPageIndex = Convert.ToInt32(PageIndex) + ;
}
break;
case "Last":
ViewBag.PIndex = PageCount;
NowPageIndex = Convert.ToInt32(PageCount);
break;
} string LastPageSize = (PageSize * (NowPageIndex - )).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分页语句获得数据。