【第十一篇】这一篇来说说MVC+EF+easyui datagrid的查询功能

老规矩

直接上代码

 1                       <form class="form-horizontal">
 2                         <div class="box-body">
 3                             <div class="row">
 4                                 <div class="form-group col-xs-1" style="width: 390px;">
 5                                     <label for="CersNo" class="col-sm-2 control-label">证书</label>
 6                                     <div class="col-sm-10">
 7                                         <input type="text" class="form-control" id="CersNo" placeholder="证书号">
 8                                     </div>
 9                                 </div>
10                                 <div class="form-group col-xs-2" style="width: 390px;">
11                                     <label for="StoneID" class="col-sm-2 control-label">货号</label>
12                                     <div class="col-sm-10">
13                                         <input type="text" class="form-control" id="StoneID" placeholder="货号">
14                                     </div>
15                                 </div>
16                             </div>
17                             <div class="row">
18                                 <div class="form-group  col-xs-1" style="width: 390px;">
19                                     <label for="size" class="col-sm-2 control-label">重量</label>
20                                     <div class="row">
21                                         <div class="col-xs-3">
22                                             <input type="text" class="form-control col-xs-3" style="width: 117px;" id="minSize" placeholder="minSize">
23                                             <hr style="width: 20px; border: solid 1px #D2D6DE; position: absolute; left: 137px; bottom: -5px;" />
24                                         </div>
25                                         <div class="col-xs-4">
26                                             <input type="text" class="form-control col-xs-4" style="width: 117px;" id="maxSize" placeholder="maxSize">
27                                         </div>
28                                     </div>
29                                 </div>
30                                 <div class="form-group  col-xs-1" style="width: 390px;">
31                                     <button type="reset" class="btn btn-default pull-right" style="width: 130px; margin-right: 15px;">重置</button>
32                                     <button type="button" onclick="onSearch()" class="btn btn-info pull-right" style="width: 130px; margin-right: 5px;">查询</button>
33                                 </div>
34                             </div>
35                         </div>
36                     </form>
37 
38 
39 <table id="proGrid">
40                         </table>

这里我想先上个效果图

我就不分步骤了,直接来代码

先是动态表格的生成,注意看column里面的值,后面我会说到为什么会这么写 

 1 <script type="text/javascript">
 2 
 3     $(function () {
 4         LoadGrid();
 5     })
 6 
 7     //加载表格!!!
 8     function LoadGrid() {
 9         $('#proGrid').datagrid({
10             width: 900,
11             striped: true,   //交替条纹
12             fitColumns: false,   //防止水平滚动
13             fit: true,//自动补全 
14             iconCls: "icon-save",//图标  
15             idField: 'UserName',  //唯一列
16             url: "GetStock",
17             dataType: "json",
18             singleSelect: true,  //设置为true将只允许选择一行
19             loadMsg: '正在拼命加载,请稍后...',
20             rownumbers: true,   //显示行数
21             pagination: true,  //底部分页工具栏  
22             nowrap: true,   //截取超出部分的数据
23             checkOnSelect: true,//点击一行的时候 checkbox checked(选择)/unchecked(取消选择)
24             pageNumber: 1,//初始化分页码。
25             pageSize: 20,  //初始化每页记录数。
26             pageList: [10, 20, 30, 50, 100, 500, 1000],   //初始化每页记录数列表
27             showFooter: false,  //定义是否显示行底
28             columns: column,
29             onLoadError: function () {
30                 layer.msg("没有查询到记录!");
31             }
32         });
33     };
34 
35     var column = [[
36             { field: "StockId", title: "编号", width: 80, align: "center", sortable: "true", hidden: true },
37             { field: "ProductId", title: "产品编号", width: 80, align: "center", sortable: "true", hidden: true },
38             { field: "ProductNum", title: "数量", width: 100, align: "center" },
39             { field: "IsOnline", title: "是否在商城", width: 100, align: "center" },
40             {
41                 field: "StoneID", title: "货号", width: 130, align: "center", formatter: function (value, row, index) {
42                     if (row.Product != null) {
43                         return row.Product.StoneID;
44                     }
45                 }
46             },
47             {
48                 field: "SuppliersId", title: "供应商编号", width: 100, align: "center", sortable: "true", formatter: function (value, row, index) {
49                     if (row.Product != null) {
50                         return row.Product.SuppliersId;
51                     }
52                 }
53             },
54             {
55                 field: "Shape", title: "形状", width: 100, align: "center", formatter: function (value, row, index) {
56                     if (row.Product != null) {
57                         return row.Product.Shape;
58                     }
59                 }
60             },

因为列太多了,我就不一一贴出来了,都是一样的。

下面的是搜索的方法,用到的是datagrid的load事件,注意带参过去的

 1   function onSearch() {
 2         if (checkInput()) {
 3             $("#proGrid").datagrid('load', {
 4                 CersNo: $("#CersNo").val().trim(),
 5                 StoneID: $("#StoneID").val().trim(),
 6                 minSize: $("#minSize").val().trim(),
 7                 maxSize: $("#maxSize").val().trim()
 8             });
 9         }
10     }

接着是控制器的代码,这里接收了参数

 1  public JsonResult GetStock(int? page, int? rows)
 2         {
 3             page = page == null ? 1 : page;
 4             rows = rows == null ? 1 : rows;
 5 
 6             string CersNo = Request["CersNo"];
 7             string StoneID = Request["StoneID"];
 8             double minSize = 0;
 9             double maxSize = 0;
10             if (!string.IsNullOrEmpty(Request.Params["minSize"]))
11             {
12                 minSize = Convert.ToDouble(Request.Params["minSize"]);
13             }
14             if (!string.IsNullOrEmpty(Request.Params["maxSize"]))
15             {
16                 maxSize = Convert.ToDouble(Request.Params["maxSize"]);
17             }
18 
19             List<Product> proList = pService.GetAllPro();
20             List<Product> prodList = new List<Product>();
21             if (proList != null)
22             {
23                 for (int i = 0; i < proList.Count; i++)
24                 {
25                     Product p = new Product();
26                     p.ProductId = proList[i].ProductId;
27                     p.StoneID = proList[i].StoneID;
28                     p.SuppliersId = proList[i].SuppliersId;
29                     p.Shape = proList[i].Shape;
30                     p.Size = proList[i].Size;
31                     ..............
32                     prodList.Add(p);
33                 }
34             }
35 
36 
37             List<Stock> produList = stService.SearchList(CersNo, StoneID, minSize, maxSize, Convert.ToInt32(page), Convert.ToInt32(rows));
38             List<Stock> pList = new List<Stock>();
39             if (proList != null)
40             {
41                 for (int i = 0; i < produList.Count; i++)
42                 {
43                     Stock p = new Stock();
44                     p.StockId = produList[i].StockId;
45                     p.ProductId = produList[i].ProductId;
46                     p.ProductNum = produList[i].ProductNum;
47                     p.IsOnline = produList[i].IsOnline == "0" ? "" : "";
48                     if (prodList!=null)
49                     {
50                         Product pr = prodList.SingleOrDefault(m => m.ProductId == produList[i].ProductId);
51                         if (pr!=null)
52                         {
53                             p.Product = pr;
54                         }
55                     }
56                     pList.Add(p);
57                 }
58                 var json = new
59                 {
60                     total = stService.GetTotal(CersNo, StoneID, minSize, maxSize),
61                     rows = pList
62                 };
63                 return Json(json, JsonRequestBehavior.AllowGet);
64             }
65             else
66             {
67                 return null;
68             }
69         }

代码看了之后,我想说说为什么我要用集合转存集合

我贴一个代码块出来

/// <summary>
        /// 搜索
        /// </summary>
        /// <param name="CersNo"></param>
        /// <param name="StoneID"></param>
        /// <param name="minSize"></param>
        /// <param name="maxSize"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public List<Stock> SearchList(string CersNo, string StoneID, double minSize, double maxSize, int page, int rows)
        {
            using (diamondEntities entity = new diamondEntities())
            {
                IQueryable<Stock> proList = null;
                if (string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize == 0 && maxSize == 0)
                {//查询全部
                    proList = entity.Stocks.OrderBy(a => a.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize != 0 && maxSize != 0)
                {//重量区间
                    proList = entity.Stocks.Where<Stock>(a => a.Product.Size >= minSize && a.Product.Size <= maxSize).OrderBy(m => m.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (!string.IsNullOrEmpty(CersNo) && !string.IsNullOrEmpty(StoneID) && minSize == 0 && maxSize == 0)
                {//证书编号和货号
                    proList = entity.Stocks.Where<Stock>(a => a.Product.CersNo.Contains(CersNo) || a.Product.CersNo2.Contains(CersNo) && a.Product.StoneID.Equals(StoneID)).OrderBy(m => m.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (!string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize != 0 && maxSize != 0)
                {//证书和重量
                    proList = entity.Stocks.Where<Stock>(a => a.Product.CersNo.Contains(CersNo) || a.Product.CersNo2.Contains(CersNo) && a.Product.Size >= minSize && a.Product.Size <= maxSize).OrderBy(m => m.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (string.IsNullOrEmpty(CersNo) && !string.IsNullOrEmpty(StoneID) && minSize != 0 && maxSize != 0)
                {//货号和重量
                    proList = entity.Stocks.Where<Stock>(a => a.Product.StoneID.Equals(StoneID) && a.Product.Size >= minSize && a.Product.Size <= maxSize).OrderBy(m => m.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (!string.IsNullOrEmpty(CersNo) && string.IsNullOrEmpty(StoneID) && minSize == 0 && maxSize == 0)
                {//只有证书
                    proList = entity.Stocks.Where<Stock>(a => a.Product.CersNo.Contains(CersNo) || a.Product.CersNo2.Contains(CersNo)).OrderBy(m => m.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (string.IsNullOrEmpty(CersNo) && !string.IsNullOrEmpty(StoneID) && minSize == 0 && maxSize == 0)
                {//只有货号
                    proList = entity.Stocks.Where<Stock>(a => a.Product.StoneID.Equals(StoneID)).OrderBy(m => m.StockId).Skip((page - 1) * rows).Take(rows);
                    List<Stock> pList = proList.ToList();
                    if (pList.Count > 0)
                    {
                        return pList;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

有没有看见,我用的using它会自动释放掉ef的资源

在程序运行时会报错  object资源已释放什么的

所以才用了另外一个集合来转存所有的数据

同时碰见了一个问题

1     public partial class Stock
2     {
3         public decimal StockId { get; set; }
4         public Nullable<decimal> ProductId { get; set; }
5         public Nullable<int> ProductNum { get; set; }
6         public string IsOnline { get; set; }
7     
8         public virtual Product Product { get; set; }
9     }

这是EF自动生成的数据模型,有一个Product的模型,我的页面主要显示的是它的所有属性

因此才会用另外的集合存起来

这个也是因为会报Object资源已释放的错

至此就完成了模糊查询  区间查询

 

 

--------------------------------------------------------------------------------------------------------- 

转载请记得说明作者和出处哦-.-
作者:KingDuDu
原文出处:https://www.cnblogs.com/kingdudu/articles/4762667.html

---------------------------------------------------------------------------------------------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值