MVCpager 用法.
先加个Nuget包 [Webdiyer.MvcPager]
@using Webdiyer.WebControls.Mvc @model PagedList<Model.User> @{ ViewBag.Title = "Users Index"; } <h2>Users Index</h2> @*此处通过RouteValueDictionary.把所要查询的参数通用URL保存起来.翻页时保留查询条件*@ @using (Html.BeginForm("Index", "Users", new RouteValueDictionary { { "id", "" } }, FormMethod.Get, new { @class = "form-inline" })) { <input type="text" id="UserName" name="UserName" placeholder="UserName" class="form-control" value="@Request.QueryString["UserName"]" /> <button id="submit1" type="submit" class="btn btn-primary"><i class="fa fa-search"></i> Search</button> <a href="~/Users/Users/Create" class="btn btn-success pull-right"><i class="fa fa-plus-circle"></i> Create New</a> } <br /> <table class="table table-hover"> <tr> <th> @Html.DisplayNameFor(model => model.UserName) </th> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.IsDisabled) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.UserName) </td> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @item.IsDisabled </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | @Html.ActionLink("Details", "Details", new { id = item.Id }) | @Html.ActionLink("Delete", "Delete", new { id = item.Id }) </td> </tr> } </table> <table> <tr> @*第几页/共几页 共几条*@ @*<td width="200px"> @Model.CurrentPageIndex / @Model.TotalPageCount All counts - @Model.TotalItemCount </td>*@ <td> @*bootstrap样式*@ <nav> @Html.Pager(Model, new PagerOptions { PageIndexParameterName = "id", NumericPagerItemCount = 5, FirstPageText = "|<", PrevPageText = "<", NextPageText = ">", LastPageText = ">|", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>", Id = "bootstrappager" }) </nav> </td> </tr> </table>
后台方法.部分:
public List<User> GetUserList(string UserName, int id) { var user = db.User.AsQueryable(); if (!string.IsNullOrEmpty(UserName)) { user = user.Where(u => u.UserName.Contains(UserName)); } //默认最多查询100条数据.超出100条.查询是没有意义的,用户不会在众多数据中手动查询 user = user.OrderBy(u => u.Id).Take(100); return user.ToPagedList(id, 10); }
本文为博主原创文章,欢迎转载,但转载须注在明显位置注明【博客地址】和【原文地址】,否则将追究法律责任。http://www.cnblogs.com/cxd1008