欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

asp.net mvc 实现分页查询

程序员文章站 2022-06-11 22:42:51
...

控制器代码

下面通过一个例子来说明:
public ActionResult Index(string Name,int PageIndex=1,int PageSize=10)
{
//根据页码每页的条数名称查询
var roles= db.Roles.Where(p=>p.Name.Contains(Name))
.OrderBy(p=>p.ID)
.Skip((PageIndex-1)*PageIndex)
.Take(PageSize)
.ToList();
return (roles);
}

视图代码

<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>  共 @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><li></ul>
    <ul class="pagination">
    
              <li><a href="javascript:page(1);">首页</a><li>
        <li><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>
 </ul>
</nav>

     

js代码

@section scripts{
    <script>
    function page(pageIndex) {
            var pageSize = $("#pageSize").val();
            var name = $("#txtCondName").val();
            window.location.href = "/Userinfo/index?pageIndex=" + pageIndex + "&pageSize=" + pageSize + "&name=" + name
                }
    </script>
    
    }

相关标签: 笔记 c#