您的位置:首页 > 其它

MCV分页实现

2015-06-01 19:47 288 查看
MVC的分页实现在Models文件夹中添加EF,添加完成后再控制器中创建数据上下文。

MicroblogEntities db = new MicroblogEntities();

创建好之后。写具体的分页代码,代码如下:

int pageindex;

if(!int.Tryparse()Request["pageindex"],out pageindex)

{

pageindex=1; //当前页

}

int pagesize=3;//每页显示的记录

// 第一页

if(pageindex<1)

{

pageindex=1;

}

//上一页

if(pageindex>1)

{

ViewDate["UP"]=pageindex-1;

}

//获取总页数

int total = db.M_user.Count();//获取总的记录数
int pagecount = 0;
if (total % pagesize == 0)
{
pagecount = total / pagesize;
}
else
{
pagecount = total / pagesize + 1;
}

//下一页

if(pageindex<pagecount)

{

pageindex=pagecount;

}

ViewData["Next"] = pageindex + 1;

//最后一页

ViewData["last"]=pagecount;

var user= db.M_user.Where<M_user>(e => true).OrderBy<M_user, int>(c => c.id).Skip<M_user>((pageindex - 1) * pagesize).Take<M_user>(pagesize);
List<M_user> list = user.ToList();
ViewData.Model = list;
return View();

//这只是其中的一个方法,只需前台遍历ViewData.Model。

下边这种是仿百度的分页样式。

可以在models文件夹中添加一个静态类,类中一个方法

public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
{
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
pageSize = pageSize == 0 ? 3 : pageSize;
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
var output = new StringBuilder();
if (totalPages > 1)
{
//if (currentPage != 1)
{//处理首页连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
}
if (currentPage > 1)
{//处理上一页的连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
}
else
{
// output.Append("<span class='pageLink'>上一页</span>");
}

output.Append(" ");
int currint = 5;
for (int i = 0; i <= 10; i++)
{//一共最多显示10个页码,前面5个,后面5个
if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
{
if (currint == i)
{//当前页处理
//output.Append(string.Format("[{0}]", currentPage));
output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
}
else
{//一般页处理
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
}
}
output.Append(" ");
}
if (currentPage < totalPages)
{//处理下一页的链接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
}
else
{
//output.Append("<span class='pageLink'>下一页</span>");
}
output.Append(" ");
if (currentPage != totalPages)
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
}
output.Append(" ");
}
output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行

return new HtmlString(output.ToString());
}

控制器中只需写和上边差不多的代码:

public ActionResult ShowuserInfo()
{
MicroblogEntities db = new MicroblogEntities();
int pageindex;
if (!int.TryParse(Request["pageindex"], out pageindex))
{
pageindex = 1;
}
int pagesize = 3;
int total = db.M_user.Count();
int pagecount = (int)Math.Ceiling((double)total / pagesize);
ViewData["last"] = pagecount;
ViewData["pageindex"] = pageindex;
ViewData["pagesize"] = pagesize;
ViewData["totalcount"] = total;
List<M_user> list = db.M_user.Where<M_user>(e => true).OrderBy<M_user, int>(c => c.id).Skip<M_user>((pageindex - 1) * pagesize).Take<M_user>(pagesize).ToList();
ViewData.Model = list;
return View();
}

最后前台绑定数据,MVC分页就实现了

如图:(没加样式)



本人小菜鸟,大神们有错请指出,小弟一定改正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: