ASP.NET MVC 查询加分页
使用了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)
ASP.NET MVC 查询加分页的更多相关文章
- 学习ASP.NET MVC(七)——我的第一个ASP.NET MVC 查询页面
在本篇文章中,我将添加一个新的查询页面(SearchIndex),可以按书籍的种类或名称来进行查询.这个新页面的网址是http://localhost:36878/Book/ SearchIndex. ...
- Web 组合查询加 分页
使用ADO.NET 数据访问技术制作web端组合查询加分页的功能关键在于查询SQL语句的拼接 以Car 表为例 每页显示3条数据 数据访问类使用查询方法,tsql 查询的连接字符串,查询的参数放到Ha ...
- ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender
(原文) 昨天在ASP.NET MVC利用PagedList分页(一)的 最后一节提到,一个好的用户体验绝对不可能是点击下一页后刷新页面,所以今天来说说利用Ajax+PagedList实现无刷新(个人 ...
- ASP.NET MVC中加载WebForms用户控件(.ascx)
原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...
- ASP.NET MVC动态加载数据
ASP.NET MVC动态加载数据,一般的做法是使用$.each方法来循环产生tabel: 你可以在html时先写下非动态的部分: Source Code 上图中,有一行代码: <tbody ...
- asp.net mvc easyui datagrid分页
提到 asp.net mvc 中的分页,很多是在用aspnetpager,和easyui datagrid结合的分页却不多,本文介绍的是利用easyui 中默认的分页控件,实现asp.net mvc分 ...
- asp.net mvc多条件+分页查询解决方案
开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...
- ASP.NET MVC利用PagedList分页(一)
前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...
- 使用Filter跟踪Asp.net MVC页面加载时间
最近,客户一直反馈系统使用慢,有时候能够指出具体是哪个页面,有时候又只是笼统地反馈慢.这种问题就像是幽灵一样,非常不好处理.因为导致这种问题的因素非常之多,而且在开发工程中,很难模拟出实际运行是的环境 ...
- 使用Filter跟踪Asp.net MVC页面加载(转)
转载地址:http://www.cnblogs.com/JustRun1983/p/4027929.html 最近,客户一直反馈系统使用慢,有时候能够指出具体是哪个页面,有时候又只是笼统地反馈慢.这种 ...
随机推荐
- HarmonyOS NEXT应用开发之深色跑马灯案例
介绍 本示例介绍了文本宽度过宽时,如何实现文本首尾相接循环滚动并显示在可视区,以及每循环滚动一次之后会停滞一段时间后再滚动. 效果图预览 使用说明: 1.进入页面,检票口文本处,实现文本首尾相接循环滚 ...
- 【产品能力深度解读】连续入围Gartner魔力象限的Quick BI有何魔力?
简介: 国际权威分析机构Gartner发布2021年商业智能和分析平台魔力象限报告,阿里云Quick BI再度入选,并继续成为该领域魔力象限唯一入选的中国企业. Quick BI凭借在增强分析能力上的 ...
- 技术干货 | mPaaS 小程序高玩带你起飞:客户端预置小程序无视网络质量
简介: 弱网拉包无障碍,深度提升用户体验 传统的小程序技术容易受到网络环境影响,当网络质量不佳时可能导致拉取不到小程序包的情况.通过预置小程序,即可规避该问题.本文介绍了预置小程序的原理和预置小程序的 ...
- Serverless 工程实践 | 细数 Serverless 的配套服务
简介: 上文说到云计算的十余年发展让整个互联网行业发生了翻天覆地的变化,Serverless 作为云计算的产物,或者说是云计算在某个时代的表现,被很多人认为是真正意义上的云计算,关于"Se ...
- [Go] gorm 返回指定模型数据的处理方式
重新 var 声明一个变量,类型为包含指定字段的结构体. 查询的时候,还是使用原始模型类型的变量. example: // For return data var retMember struct { ...
- 阿里巴巴Canal常见问题:重复解析/Filter失效/消费落后
前言 Canal是阿里巴巴开源的数据库Binlog日志解析框架,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费. 在之前我写的文章阿里开源MySQL中间件Canal快速入门中, ...
- Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value)(intermediate value)...is not a function
之前写完前端部分的时候总是会出现这个错误 对应代码 解决方法,在后面加上分号即可解决
- 深入理解 Swift Combine
Combine 文中写一些 Swift 方法签名时,会带上 label,如 subscribe(_ subscriber:),正常作为 Selector 的写法时会忽略掉 label,只写作 subs ...
- ansible系列(21)--ansible的变量注册Register
1. 变量注册Register register 关键字可以将某个 task 任务结果存储至变量中,最后使用 debug模块 输出变量内容,可以用于后续排障: 示例一:register的基本使用: # ...
- vue 安装教程(如何在node环境下搭建vue项目)
如果要配置node.js环境请查看(node.js环境在Window和Mac中配置,以及安装cnpm和配置Less环境)在终端输入命令 cnpm -v出现版本号 1.安装vue window : ...