EF中的分页
定义,当前页,每页显示都少条,已经搜索的内容。
控制器内容:
默认认为,当前为第一页,每页显示10条数据,显示全部内容。
public ActionResult Index(int pageIndex=1,int pageSize=10,string Name="")
{
var counts = db.UserInfos.Where(p => p.Name.Contains(Name)).Count();
var totalpage = Math.Ceiling(counts * 1.00 / pageSize);
var row = db.UserInfos.Where(p => p.Name.Contains(Name))
.OrderBy(p => p.ID)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).ToList();
ViewBag.pageIndex = pageIndex;
ViewBag.totalpage = totalpage;
ViewBag.pageSize = pageSize;
ViewBag.Name = Name;
return View(row);
}
视图内容:
搜索框:
<div style="display:flex;justify-content:space-between">
<div class="btn-group">
<button type="button" class="btn btn-default">新增</button>
<button type="button" class="btn btn-default">删除</button>
</div>
<div class="input-group">
<label>名称</label>
<input type="text" id="txtCondName" value="@ViewBag.Name"/>
<input type="button" value="搜索" id="btnSearch" onclick="page(1);" />
</div>
</div>
表格显示数据:
<table class="table table-bordered">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>备注</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Remark</td>
</tr>
}
</tbody>
</table>
分页样式:
<nav aria-label="Page navigation" style="display:flex;justify-content:space-between">
<ul class="pagination">
<li>
<a href="#">
共 @ViewBag.totalpage 页,第<input type="text" id="pageIndex" value="@ViewBag.pageIndex" />页,
每显示
<select id="pageSize" onchange="page(1);">
@{
var pageSizes = new List<int> { 5, 10, 20, 50 };
}
@foreach (var item in pageSizes)
{
if (@ViewBag.pageSize == @item)
{
<option value="@item" selected="selected">@item</option>
}
else
{
<option value="@item">@item</option>
}
}
</select>条
</a>
</li></ul>
<ul class="pagination">
<li><a href="javascript:page(1);">首页</a></li>
@if (ViewBag.pageIndex > 1)
{
<li><a href="javascript:page(@ViewBag.pageIndex-1)">上页</a></li>
}
else
{
<li class="disabled"><a href="javascript:page(@ViewBag.pageIndex-1)">上页</a></li>
}
<li><a href="javascript:page(@ViewBag.pageIndex+1)">下页</a></li>
<li><a href="javascript:page(@ViewBag.totalpage)">末页</a></li>
<li><input type="button" value="go" onclick="page(1)" class="btn btn-default" /></li>