使用了LinqKit、PagedList.Mvc、EntityFramework 等DLL 直接使用nuget安装即可。
1.表模型:
using System.ComponentModel.DataAnnotations; namespace StoreDB.Models { public class t_bd_item_info { [Key] //主键 public string item_no { get; set; } public string item_name { get; set; } public decimal price { get; set; } //public DateTime? build_date { get; set; } } }
DbContext:
using StoreDB.Models; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; namespace StoreDB { public class StoreDbContext: DbContext { public StoreDbContext() : base("name=StoreDbContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //移除复数表名 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); base.OnModelCreating(modelBuilder); } public virtual DbSet<t_bd_item_info> t_bd_item_info { get; set; } } }
2.HomeController增加一个SearchIndex Action:
using LinqKit; using PagedList; using StoreDB; using StoreDB.Models; using System; using System.Linq; using System.Linq.Expressions; using System.Web.Mvc; namespace WebQueryAndPage.Controllers { public class HomeController : Controller { public ActionResult Index() { return Redirect("/Home/SearchIndex"); //return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } public ActionResult SearchIndex(string ItemNo, string ItemName, int page = 1) { try { //为true 默认加载所有 Expression<Func<t_bd_item_info, bool>> where = PredicateBuilder.New<t_bd_item_info>(true); //动态拼接查询条件 if (!string.IsNullOrWhiteSpace(ItemNo)) { where = where.And(x => x.item_no == ItemNo); } if (!string.IsNullOrWhiteSpace(ItemName)) { where = where.And(x => x.item_name.Contains(ItemName)); } StoreDbContext dbContext = new StoreDbContext(); //直接ToPagedList,不要使用ToList var mylist = dbContext.t_bd_item_info.Where(where).OrderBy(x => x.item_no).ToPagedList(page, 10); return View(mylist); } catch (Exception ex) { return Content(ex.Message); } } } }
这里使用到了PredicateBuilder (LinqKit),来动态拼接查询条件。ToPagedList (PagedList)来分页。
3.为SearchIndex Action 增加一个视图SearchIndex.cshtml页面:
@*@model IEnumerable<StoreDB.Models.t_bd_item_info>*@ @model IPagedList<StoreDB.Models.t_bd_item_info> @using PagedList; @using PagedList.Mvc; @{ ViewBag.Title = "SearchIndex"; } <h2>SearchIndex</h2> @using (Html.BeginForm("SearchIndex", "Home", FormMethod.Get, new { id = "OrderForm" })) { <p> 货号: @Html.TextBox("ItemNo") 商品名称: @Html.TextBox("ItemName") <input type="submit" value="查询" /> </p> } <table class="table"> <tr> <th> 货号 </th> <th> 商品名称 </th> <th> @*@Html.DisplayNameFor(model => model.price)*@ 单价 </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.item_no) </td> <td> @Html.DisplayFor(modelItem => item.item_name) </td> <td> @Html.DisplayFor(modelItem => item.price) </td> <td> @*@Html.ActionLink("Edit", "Edit", new { id=item.item_no }) | @Html.ActionLink("Details", "Details", new { id=item.item_no }) | @Html.ActionLink("Delete", "Delete", new { id=item.item_no })*@ </td> </tr> } </table> @Html.PagedListPager(Model, page => Url.Action("SearchIndex", new { page, ItemNo = Request["ItemNo"], ItemName = Request["ItemName"] }))
从IEnumerable<StoreDB.Models.t_bd_item_info> 改成 IPagedList<StoreDB.Models.t_bd_item_info>,需要引用 @using PagedList;、@using PagedList.Mvc;
@Html.DisplayNameFor 无法使用,直接写名称。
底部增加@Html.PagedListPager:
@Html.PagedListPager(Model, page => Url.Action("SearchIndex", new { page, ItemNo = Request["ItemNo"], ItemName = Request["ItemName"] }))
其中page是当前页面, ItemNo和ItemName是表单的查询条件。如果不加“ItemNo = Request["ItemNo"], ItemName = Request["ItemName"] ”,点击下一页时,查询条件就变为空了。
源码:donetproj/MVC查询和分页 · runliuv/mypub - 码云 - 开源中国 (gitee.com)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2016-12-13 碧水剑:+4时出的速度+3,+6时出的速度+6,+7时出的速度+9。
2016-12-13 检测到应用叠加层
2013-12-13 datacolumn 表达式 除数为0