位置 :  首页 / 博客 / 学无止境

ASP.NET MVC分页设置

发布人:南昌开优网络 发布时间:2017/08/08 访问量:3024
控制器Controllers
public ActionResult Index(int pageIndex = 1)
{  
	if (pageIndex < 1)
	{
		pageIndex = 1;  //最少为第一页
	}
	int pageSize = 10;  //每页显示数
	string cond = "";  //条件
	int total = new DAL.ArticleDAL().CalcCount(cond);  //总记录           
	List<Model.Article> list = new DAL.ArticleDAL().GetListArray("*", "sort_id asc,createdate desc,id desc", pageSize, pageIndex, cond);  //获取数据列表,即分页函数
	PagedList<Model.Article> mlist = new PagedList<Model.Article>(list, pageIndex, pageSize, total);  //分页        
	return View(mlist);  //返回列表
}

视图Views
@using Webdiyer.WebControls.Mvc;
@model PagedList<XiaobinManage.Model.Article>

@{
    //ViewBag.title = "新闻列表";
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.title</title>
    <link href="/css/page.css" rel="stylesheet" />
</head>
<body>
    @foreach (var item in Model)
    {
        <div>
            @item.title - @item.createdate.ToString("yyy-MM-dd")
        </div>
    }
    <div class="pagesDiv">
        @*@Model.TotalItemCount 总记录数,
          @Model.CurrentPageIndex 当前页,
          @Model.TotalPageCount 总页数,
          PageIndexParameterName = "pageIndex" 页索引参数名称,
          NumericPagerItemCount = 5 显示数字按钮个数,
          ShowMorePagerItems = false 是否显示更多, 
          CurrentPagerItemTemplate = "<span class=\"cpb\">{0}</span>" 当前页的样式, 
          DisabledPagerItemTemplate = "<a disabled=\"disabled\">{0}</a>" 禁用页索引的样式 *@
        <span style="float: left;">共 @Model.TotalItemCount 条记录,第 @Model.CurrentPageIndex 页/共 @Model.TotalPageCount 页</span>
        @Html.Pager(Model, new PagerOptions { PageIndexParameterName = "pageIndex", CssClass = "pages", NumericPagerItemCount = 5, ShowMorePagerItems = false, CurrentPagerItemTemplate = "<span class=\"cpb\">{0}</span>", DisabledPagerItemTemplate = "<a disabled=\"disabled\">{0}</a>" })
    </div>
</body>
</html>