runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  710 随笔 :: 0 文章 :: 127 评论 :: 98万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

使用了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) 

 

posted on   runliuv  阅读(168)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-12-13 碧水剑:+4时出的速度+3,+6时出的速度+6,+7时出的速度+9。
2016-12-13 检测到应用叠加层
2013-12-13 datacolumn 表达式 除数为0
点击右上角即可分享
微信分享提示