博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
datagrid在MVC中的运用02-结合搜索
阅读量:4652 次
发布时间:2019-06-09

本文共 3706 字,大约阅读时间需要 12 分钟。

本文接着上一篇,来体验给datagrid加上搜索功能。主要涉及到:

※ 把一个div与datagrid相关起来

※ datagrid接收查询参数
※ 查询参数的封装

效果图:

  查询参数封装

分页相关的是每个页面都能用到的,所以把分页相关的封装成基类。

与查询相关的,封装成继承基类的子类。

public class PageParam
{
public int PageSize { get; set; }
public int PageIndex { get; set; }
}
 
public class BookParam : PageParam
{
public string ItemId { get; set; }
public string ProductId { get; set; }
}

  查询Book的服务类方法就要考虑分页和查询参数

展开using System.Linq; using DataGridInMvc.Models; using System.Collections.Generic; using Microsoft.Ajax.Utilities;namespace DataGridInMvc.Helper {     public class BookService     {         public IEnumerable
LoadPageBookData(BookParam param, out int total) { //创建Book的集合 var books = new List
(); for (int i = 0; i < 30; i++) { books.Add(new Book() { ItemId = "EST-" + i, ProductId = "AV-SB-" + i, ListPrice = (i + 5) * 10m, UnitCost = (i + 2) * 10m, Attr1 = "Attr" + i, Status = (short)0 }); } //搜索逻辑 if (!string.IsNullOrEmpty(param.ItemId)) { books = books.Where(b => b.ItemId.Contains(param.ItemId)).ToList(); } if (!string.IsNullOrEmpty(param.ProductId)) { books = books.Where(b => b.ProductId.Contains(param.ProductId)).ToList(); } total = books.Count(); var result = books.OrderBy(b => b.ItemId) .Skip(param.PageSize*(param.PageIndex - 1)) .Take(param.PageSize); return result; } } }

 

  视图

datagrid的toolbar属性能把div与datagrid相关起来,并显示到datagrid的顶部。

dtagrid的queryParams属性能接受查询参数,传递给Controller.
点击查询按钮的时候,需要带上参数。

展开    

选择分页显示位置: 选择分页显示样式

主键:
产品编号:
搜索

  Controller

展开using System.Web; using System.Web.Mvc; using DataGridInMvc.Helper; using DataGridInMvc.Models;namespace DataGridInMvc.Controllers {     public class HomeController : Controller     {         public ActionResult Index()         {             return View();         }        //         public ActionResult GetData()         {             //接收datagrid传来的参数             int pageIndex = int.Parse(Request["page"]);             int pageSize = int.Parse(Request["rows"]);            //接收搜索参数             string itemId = Request["ItemId"];             string productId = Request["ProductId"];            //构建得到分页数据方法所需的参数             var temp = new BookParam()             {                 PageIndex = pageIndex,                 PageSize = pageSize,                 ItemId = itemId,                 ProductId = productId             };            //分页数据方法的输出参数             int totalNum = 0;            var service = new BookService();             var books = service.LoadPageBookData(temp, out totalNum);            var result = from book in books                 select new {book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1};            //total,rows是前台datagrid所需要的             var jsonResult = new {total = totalNum, rows = result};            //把json对象序列化成字符串             string str = JsonSerializeHelper.SerializeToJson(jsonResult);             return Content(str);         }     } }

 

在上一篇介绍过。

转载于:https://www.cnblogs.com/darrenji/p/3573375.html

你可能感兴趣的文章
HTML框架、选择器、列表
查看>>
60行JS实现俄罗斯方块
查看>>
php以及前端的一些小小的技术要点
查看>>
html基础知识
查看>>
IO流2之文件夹的
查看>>
好的用户界面-界面设计的一些技巧
查看>>
【Android实战开发】3G技术和Android发展简介
查看>>
【精解】EOS标准货币体系与源码实现分析
查看>>
AFore.NET 翻译
查看>>
[大牛翻译系列]Hadoop(8)MapReduce 性能调优:性能测量(Measuring)
查看>>
What to do when the Chinese Characters are messed up when extracting from zip archive?
查看>>
SQLYog快捷键大全
查看>>
(转载)DLL动态链接库编程入门之三:MFC规则DLL(上)
查看>>
隐藏Nginx或Apache以及PHP的版本号的方法
查看>>
N32926之jlink调试配置
查看>>
ASP.NET ACCESS 分页
查看>>
HashMap
查看>>
Android广播机制概述
查看>>
mysql触发器
查看>>
我是怎么让全国最大的儿童失踪预警平台流量掉底的
查看>>