MVC3分页
引用MVCPaging.dll来进行分页,不过不同的MVC的版本一定要下载不同的MVCPaging.dll来使用。否则在MVC3里面会不兼容的。
Controller:
using MvcPaging;
public ActionResult Index(int? page)
{
var list = VideoRepository.FindAll().OrderByDescending(v => v.CreateTime);
return View(list.ToPagedList(page.HasValue ? page.Value - 1 : 0, 3));
}
View:
@model IPagedList<Funny.Models.Video>
@using MvcPaging;
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>
Video<span>Some of our favorite Big Cartel stores.</span></h1>
<div id="main">
<ul class="block-video">
@foreach (var item in Model)
{
<li><a href="#">
<img src="/Content/videos/@item.ImageName" alt="picture" /></a>
<h4>
@item.Title</h4>
<p>
<a href="#">@item.Description</a>
</p>
</li>
}
</ul>
<div class="pager">
@Html.Pager(Model.PageSize,Model.PageNumber,Model.TotalItemCount)
</div>
</div>
Last,don't forget fix css style.
/* pager */
.pager
{
margin:8px 30px;
padding:3px;
}
.pager .disabled
{
border:1px solid #ddd;
color:#999;
margin-top:4px;
padding:3px;
text-align:center;
}
.pager .current
{
border:0px hidden;
font-weight:bold;
margin-top:4px;
padding:3px 5px;
text-align:center;
}
.pager span, .pager a
{
margin: 4px 3px;
}
.pager a
{
border:1px solid #c0c0c0;
padding:3px 5px;
text-align:center;
text-decoration:none;
}