分页基础概念与ASP.NET实现优势
分页是将大量数据按固定数量(如每页10条)拆分到多个页面中展示,用户通过翻页控件(如“上一页”“下一页”“页码”按钮)快速切换页面,ASP.NET的优势在于其灵活性和框架集成性:内置控件(如GridView)提供开箱即用的分页功能,自定义逻辑则适用于复杂场景(如结合业务逻辑的分页),本实例采用自定义分页方式,结合Entity Framework实现数据访问,确保代码的可维护性与扩展性。

项目环境搭建与数据准备
- 数据库创建
使用SQL Server创建示例数据库(如DemoDB),设计Products表(包含Id、Name、Price等字段),并插入若干测试数据。 - 实体模型设计
采用Entity Framework Code First定义实体类,封装数据库表结构:public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } - 数据访问上下文
创建AppDbContext类,包含Products集合,作为数据访问的入口:public class AppDbContext : DbContext { public DbSet<Product> Products { get; set; } }
分页逻辑实现步骤
数据访问层封装
编写方法获取总记录数和分页数据,避免重复代码:public class ProductRepository { private readonly AppDbContext _context; public ProductRepository(AppDbContext context) { _context = context; } public int GetTotalProducts() => _context.Products.Count(); public IEnumerable<Product> GetProductsByPage(int pageIndex, int pageSize) { return _context.Products .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToList(); } }控制器处理请求
在ProductsController中注入数据访问层,接收分页参数(pageIndex、pageSize),调用方法并返回视图:
public class ProductsController : Controller { private readonly ProductRepository _repository; public ProductsController(ProductRepository repository) { _repository = repository; } public IActionResult Index(int? pageIndex, int? pageSize) { const int defaultPageSize = 10; var currentPageIndex = pageIndex ?? 1; var currentPageSize = pageSize ?? defaultPageSize; var totalProducts = _repository.GetTotalProducts(); var totalPages = (int)Math.Ceiling(totalProducts / (double)currentPageSize); var products = _repository.GetProductsByPage(currentPageIndex, currentPageSize); var viewModel = new ProductsViewModel { Products = products, CurrentPageIndex = currentPageIndex, PageSize = currentPageSize, TotalPages = totalPages, TotalProducts = totalProducts }; return View(viewModel); } }视图呈现与分页控件
使用Razor语法绑定数据并渲染分页控件,实现页面跳转:@model ProductsViewModel <h2>产品列表</h2> <table class="table"> <thead> <tr> <th>ID</th> <th>名称</th> <th>价格</th> </tr> </thead> <tbody> @foreach (var product in Model.Products) { <tr> <td>@product.Id</td> <td>@product.Name</td> <td>@product.Price</td> </tr> } </tbody> </table> <!-- 分页控件 --> <nav aria-label="Page navigation"> <ul class="pagination"> <li class="page-item disabled"> <a class="page-link" href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> @for (int i = 1; i <= Model.TotalPages; i++) { <li class="page-item @(i == Model.CurrentPageIndex ? "active" : "")"> <a class="page-link" href="@Url.Action("Index", new { pageIndex = i, pageSize = Model.PageSize })">@i</a> </li> } <li class="page-item disabled"> <a class="page-link" href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav>
运行与效果验证
- 配置数据库连接
在Web.config中添加数据库连接字符串:<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=DemoDB;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> - 启动项目测试
访问/Products/Index页面,验证分页效果:每页显示10条数据,通过翻页控件切换页面,总页数与实际数据量(如100条数据则为10页)匹配。
常见问题解答(FAQs)
Q:如何调整每页显示的数据数量?
A:在控制器中修改默认PageSize值(如从配置文件读取),并在视图分页链接中传递pageSize参数,
// 控制器中 var currentPageSize = pageSize ?? 20; // 视图中 <a class="page-link" href="@Url.Action("Index", new { pageIndex = i, pageSize = 20 })">@i</a>Q:分页参数如何传递给控制器?
A:通过URL参数传递(如Index?pageIndex=2&pageSize=20),控制器中的Index方法接收int? pageIndex和int? pageSize参数,并处理null值(使用默认值)。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/205361.html


