使用了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. 力扣172(java)-阶乘后的零(中等)

    题目: 给定一个整数 n ,返回 n! 结果中尾随零的数量. 提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1 示例 1: 输入:n = 3输出:0解释: ...

  2. 迁移 Nacos 和 ZooKeeper,有了新工具

    简介: 注册中心迁移在行业中主要有两个方案,一个是双注册双订阅模式(类似数据库双写),一个是 Sync 模式(类似于数据库 DTS):MSE 同时支持了两种模式,对于开通 MSE 服务治理客户,MSE ...

  3. 【SIGIR 2022】面向长代码序列的Transformer模型优化方法,提升长代码场景性能

    简介: 论文主导通过引入稀疏自注意力的方式来提高Transformer模型处理长序列的效率和性能 阿里云机器学习平台PAI与华东师范大学高明教授团队合作在SIGIR2022上发表了结构感知的稀疏注意力 ...

  4. DataWorks开发ODPS SQL开发生产环境自动补全ProjectName

    简介: DataWorks标准模式下,支持开发环境和生产环境隔离,开发环境和生产环境的数据库表命名有所区别,如果需要在开发环境访问生产环境的数据库表或者跨项目空间访问其他项目空间的表,需要根据proj ...

  5. 使用Databricks+Mlflow进行机器学习模型的训练和部署【Databricks 数据洞察公开课】

    简介: 介绍如何使用Databricks和MLflow搭建机器学习生命周期管理平台,实现从数据准备.模型训练.参数和性能指标追踪.以及模型部署的全流程. 作者:李锦桂   阿里云开源大数据平台开发工程 ...

  6. 阿里开源支持10万亿模型的自研分布式训练框架EPL(EasyParallelLibrary)

    ​简介:EPL背后的技术框架是如何设计的?开发者可以怎么使用EPL?EPL未来有哪些规划?今天一起来深入了解. ​ 作者 | 王林.飒洋 来源 | 阿里技术公众号 一 导读 最近阿里云机器学习PAI平 ...

  7. 网不好怎么办?TLS握手带宽直降80%,BabaSSL是怎么做到的?| 龙蜥技术

    ​简介:为了保障数据的安全性,客户端会先和服务器进行 TLS 握手,有什么办法可以减少 TLS 握手的带宽消耗呢? 编者按:BabaSSL 是一款开源的密码库产品,在 GitHub 和龙蜥社区开源,并 ...

  8. python使用pysql操作MySQL数据库

    前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11. ...

  9. ES_CCS/R(二):跨集群搜索 Cross-cluster search (CCS)

    跨集群搜索(cross-cluster search)使你可以针对一个或多个远程集群运行单个搜索请求. 例如,你可以使用跨集群搜索来筛选和分析存储在不同数据中心的集群中的日志数据. 示例 :在一个集群 ...

  10. C#中的对象深拷贝和浅拷贝

    目录 C#中的对象深拷贝和浅拷贝 概述 1. 浅拷贝 2. 深拷贝 总结 引用 C#中的对象深拷贝和浅拷贝 概述 在C#中,对象拷贝是指将一个对象的副本创建到另一个对象中.对象拷贝通常用于数据传输或创 ...