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 最近,客户一直反馈系统使用慢,有时候能够指出具体是哪个页面,有时候又只是笼统地反馈慢.这种 ...
随机推荐
- MaxCompute 存储设计
简介: 存储策略该怎么设计 写这篇存储规划的文章主要是想告诉大家该如何给存储做一个规划,在关系数据库的时代存储昂贵且珍惜,掰手指头花钱是存储规划的常态.但是到了大数据时代大家又立即就都变成印美元的 ...
- [Cloud] From Borg to Kubernetes
Borg System Architect: Kubernetes System Architect: Link:https://www.cnblogs.com/farwish/p/12751861. ...
- dotnet 使用 CsWin32 库简化 Win32 函数调用逻辑
很多开发者,包括开发老司机们,在碰到需要调用 Win32 函数时,都有一个困扰,那就是我应该如何去调用.有两个主要的选项,第一就是自己写 PInvoke 代码,第二就是使用其他大佬给许多 Win32 ...
- dotnet OpenXML 解析 PPT 图表 解析日期时间表示内容
在 OpenXML 里的图表存放的日期时间很多存放的是数值,需要进行转换才能获取到用户可见的字符串 日期在 OpenXML 里可以使用数值存放,需要根据数值和格式化字符串转换为用户可见的字符串 如以下 ...
- dotnet 读 WPF 源代码笔记 提升调试效率的 NamedObject 类型
本文来聊聊 WPF 那些值得称赞的设计中的 NamedObject 类型.在 WPF 中,有很多值得我学习的设计开发思想,其中就包括本文将要介绍的 NamedObject 类型.此类型的定义仅仅只是为 ...
- containerd基本使用命令
一.containerd简介 官 方 文 档 : https://containerd.io 在 2016 年 12 月 14 日,Docker 公司宣布将containerd 从 Docker 中分 ...
- RT-Thread线程管理
一.概述 这是我学习RT-Thread线程管理过程中记录的笔记,方便自己查看,仅供参考,有什么不对的地方忘各位大佬指出.想要了解更详细的内容,请浏览官方文档"线程管理" 如下图所示 ...
- Solution Set - LCT
A[洛谷P3690]维护一个森林,支持询问路径xor和,连边(已连通则忽略),删边(无边则忽略),改变点权. B[洛谷P3203]\(n\)个装置编号为\(0,...,n-1\),从\(i\)可以一步 ...
- FFmpeg开发笔记(十九)FFmpeg开启两个线程分别解码音视频
同步播放音视频的时候,<FFmpeg开发实战:从零基础到短视频上线>一书第10章的示例程序playsync.c采取一边遍历一边播放的方式,在源文件的音频流和视频流交错读取的情况下,该方式 ...
- docker 权限问题 Got permission denied while trying to connect to the Docker daemon socket at 。。。
非root用户运行docker命令报如下错误 说明没有权限 haima@haima-PC:/usr/local/docker/docker_compose_efk$ docker ps -a Got ...