使用了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. 力扣1070(MySQL)-产品销售分析Ⅲ(中等)

    题目: 销售表 Sales: 产品表 Product: 编写一个 SQL 查询,选出每个销售产品 第一年 销售的 产品 id.年份.数量 和 价格. 结果表中的条目可以按 任意顺序 排列. 查询结果格 ...

  2. OpenYurt 开箱测评 | 一键让原生 K8s 集群具备边缘计算能力

    作者| 郑超 阿里云高级开发工程师 随着物联网技术以及 5G 技术的高速发展,将云计算的能力延伸至边缘设备端,并通过中心进行统一交付.管控,已成为云计算的重要发展趋势.为服务更多开发者把握这一趋势,5 ...

  3. 让微服务开源更普惠,阿里云微服务引擎MSE全球开服

    ​简介:MSE 于2020年10月在国内开启商业化服务,目前已吸引近万客户使用,用于在云上更低成本构建.更稳定运行微服务架构.此次,MSE 向阿里云国际站开放服务,旨在帮助更多客户享受到更加普惠的微服 ...

  4. 简说Python之ipython的pdb调试

    目录 简说Python之ipython 1.安装ipython 2.ipython的使用 3.ipython的debug调试. 系统环境:Ubuntu 18.04.1 LTS Python使用的是虚拟 ...

  5. 面向教师的OBS直播速成教程

    引言 本文是面向教师讲述的如何使用OBS软件进行课程直播的速成教程. 本文配套视频链接如下️ 面向教师的OBS直播教学速成教程_哔哩哔哩_bilibili 环境准备 1. 下载对应本机系统版本的并安装 ...

  6. elasticsearch02-Request Body深入搜索

    目录 02. Request Body深入搜索 1.1 term查询 1.1.1 term 与 terms 1.1.2 range 范围查询 1.1.3 Constant Score 1.2 全文查询 ...

  7. systemctl管理自定义服务模版

    一  日常工作中,有许多需要使用systemctl进行管理服务的时候 [Unit] Description=radius Release [Service] ExecStart=/etc/init.d ...

  8. 京东二面:Sychronized的锁升级过程是怎样的

    引言 Java作为主流的面向对象编程语言,提供了丰富的并发工具来帮助开发者解决多线程环境下的数据一致性问题.其中,内置的关键字"Synchronized"扮演了至关重要的角色,它能 ...

  9. C# 使用 运算符重载 隐式转换 对Point进行加减计算

    运算符重载方便了我们对自定义类型(自定义的类或者结构体)的计算. 运算符重载关键字 operator. 除了这些运算符不支持:x.y.f(x).new.typeof.default.checked.u ...

  10. EL表达式与JSTL简单入门

    更多博文请关注:听到微笑的博客 EL概述 EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写. 简单来说EL表达式 ...