版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014119694/article/details/76524653
首先得package pagedlist.mvc包,然后在action里和EF查询页里using PagedList
1.编写EF分页查询
public IPagedList<TEntity>GetPaged(Func<TEntity,bool>where,Func<TEntity,object>order,int pageIndex,int pageSize,bool isasc)
{
if (isasc)
return netDiskContext.Set<TEntity>().Where<TEntity>(where).OrderBy(order).ToPagedList(pageIndex, pageSize);
else
return netDiskContext.Set<TEntity>().Where<TEntity>(where).OrderByDescending(order).ToPagedList(pageIndex, pageSize);
}
2. controll action
public ActionResult Index(string path,int? page)
{
if (Session["path"] == null) Session["path"] = "\\";
if(path!=null)
{
path = path.Trim()+'\\' ;
Session["path"]=Session["path"]+ path;
}
int pages = page ?? 1;
path = Session["path"].ToString();
string currrentUser = Session["user"].ToString();
User user=userinfo.Find(x=>x.Username==currrentUser);
@ViewBag.filepath = Session["path"];
IPagedList<lengxia.Model.File> ss = fileinfo.GetPaged(x => x.Belong == user.Id && x.FilePath == path,x=>x.Id, pages, 10,true);
return View(ss);
}
3.前端实现
@model PagedList.IPagedList<lengxia.Model.File>
@using PagedList.Mvc;
<table class="table table-hover">
<tr>
<th>ID</th>
<th>文件名</th>
<th>文件路径</th>
<th>所属用户</th>
<th>操作</th>
</tr>
@foreach (var item in Model)
{
<tr>
<th>@item.Id</th>
@if (@item.FileExt =="folder")
{
<td><a href="javascript:void(0)" onclick="jump(this)" id="openfolder">@item.FileName</a></td>
}
else
{
<td>@item.FileName</td>
}
<td>@item.FilePath</td>
<td>@item.Belong</td>
<td><a href="javascript:void(0)" fileid="@item.Id" onclick="tixing(this)" fileExt="@item.FileExt">删除</a>
/<a href="javascript:void(0)" fileid="@item.Id" onclick="rename(this)" filename="@item.FileName">修改</a></td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("Index",new{page}))