使用了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 查询加分页的更多相关文章

  1. 学习ASP.NET MVC(七)——我的第一个ASP.NET MVC 查询页面

    在本篇文章中,我将添加一个新的查询页面(SearchIndex),可以按书籍的种类或名称来进行查询.这个新页面的网址是http://localhost:36878/Book/ SearchIndex. ...

  2. Web 组合查询加 分页

    使用ADO.NET 数据访问技术制作web端组合查询加分页的功能关键在于查询SQL语句的拼接 以Car 表为例 每页显示3条数据 数据访问类使用查询方法,tsql 查询的连接字符串,查询的参数放到Ha ...

  3. ASP.NET MVC利用PagedList分页(二)PagedList+Ajax+JsRender

    (原文) 昨天在ASP.NET MVC利用PagedList分页(一)的 最后一节提到,一个好的用户体验绝对不可能是点击下一页后刷新页面,所以今天来说说利用Ajax+PagedList实现无刷新(个人 ...

  4. ASP.NET MVC中加载WebForms用户控件(.ascx)

    原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...

  5. ASP.NET MVC动态加载数据

    ASP.NET MVC动态加载数据,一般的做法是使用$.each方法来循环产生tabel: 你可以在html时先写下非动态的部分:  Source Code 上图中,有一行代码: <tbody ...

  6. asp.net mvc easyui datagrid分页

    提到 asp.net mvc 中的分页,很多是在用aspnetpager,和easyui datagrid结合的分页却不多,本文介绍的是利用easyui 中默认的分页控件,实现asp.net mvc分 ...

  7. asp.net mvc多条件+分页查询解决方案

    开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...

  8. ASP.NET MVC利用PagedList分页(一)

    前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...

  9. 使用Filter跟踪Asp.net MVC页面加载时间

    最近,客户一直反馈系统使用慢,有时候能够指出具体是哪个页面,有时候又只是笼统地反馈慢.这种问题就像是幽灵一样,非常不好处理.因为导致这种问题的因素非常之多,而且在开发工程中,很难模拟出实际运行是的环境 ...

  10. 使用Filter跟踪Asp.net MVC页面加载(转)

    转载地址:http://www.cnblogs.com/JustRun1983/p/4027929.html 最近,客户一直反馈系统使用慢,有时候能够指出具体是哪个页面,有时候又只是笼统地反馈慢.这种 ...

随机推荐

  1. PS(Photoshop CC2019)安装教程

    记录一下自己安装PS2019版本的安装过程~ 先获取安装资料: 百度网盘链接: 链接:https://pan.baidu.com/s/15tzmq-6JQCdVn378ZFqXJA?pwd=997y  ...

  2. 基于信通院 Serverless 工具链模型的实践:Serverless Devs

    简介: Serverless Devs 作为开源开放的开发者工具,参编中国信通院<基于无服务器架构的工具链能力要求>标准,为行业统一规范发挥助推作用!​ 作者 | 江昱(阿里云 Serve ...

  3. Flutter+FaaS一体化任务编排的思考与设计

    作者:闲鱼技术-古风 Flutter+Serverless三端一体研发架构,客户端不仅仅是编写双端的代码,而是扩展了客户端的工作边界,形成完整的业务闭环.在新的研发模式落地与实践的过程中,一直在思考如 ...

  4. Spring Cloud Gateway 突发高危漏洞,下一代云原生网关恰逢其时?

    ​简介:Log4j2 的漏洞刚告一段落,Spring 官方在 2022 年 3 月 1 日发布了 Spring Cloud Gateway 的两个 CVE 漏洞:分别为 CVE-2022-22946( ...

  5. V8 编译浅谈

    ​简介:本文是一个 V8 编译原理知识的介绍文章,旨在让大家感性的了解 JavaScript 在 V8 中的解析过程. ​ 作者 | 子弈 来源 | 阿里技术公众号 一 简介 本文是一个 V8 编译原 ...

  6. [Kali] Kali Linux 环境准备

      虚拟机和系统: Mac 的 Vmware Fusion:https://www.vmware.com/cn/products/fusion/fusion-evaluation.html  序列号去 ...

  7. [ML] 机器学习简介

    监督学习(Supervised Learning) 添加标签,手把手训练. 比如线性回归算法. 半监督学习(Semi-supervised Learning) 非监督学习(Unsupervised L ...

  8. Django高级表单处理与验证实战

    title: Django高级表单处理与验证实战 date: 2024/5/6 20:47:15 updated: 2024/5/6 20:47:15 categories: 后端开发 tags: D ...

  9. 简易版跳板机-teleport使用

    目录 1 环境搭建 2 teleport工具搭建 3 teleport使用示例 3.1 资产管理-添加主机 3.2 资产管理-添加账号 3.3 创建用户 3.4 运维授权 3.5 安装客户端助手 3. ...

  10. nim 3. 各种集合

    其实我挺想先去学习一下nim的模块系统,毕竟我决定暂时放弃学习golang,就是因为感觉他的模块和包方面的设计,不像c#+nuget的组合那么舒服. 但是这里还是先学习一下集合吧,感觉这方面nim很有 ...