当前位置:Gxlcms > asp.net > ASP.NET MVC使用EasyUI的datagrid多选提交保存教程

ASP.NET MVC使用EasyUI的datagrid多选提交保存教程

时间:2021-07-01 10:21:17 帮助过:13人阅读

需要实现EasyUI的datagrid组件加入选择checkbox列,并提交后台批量添加的功能,页面代码如下:
代码如下:
  1. <br><script language="javascript" type="text/javascript"> <br>$(function() { <br>//searchbox <br>$('#selectgoods-keywords').searchbox({ <br>searcher: function(val, name) { <br>searchInfo(val); <br>} <br>}); <br>//datagrid <br>$('#selectgoods-grid').datagrid({ <br>url: '/Goods/List', <br>pageNumber: 1, <br>pageSize: 20, <br>pageList: [20, 40, 60, 80, 100] <br>}); <br>//form <br>}); <br>function searchInfo(val){ <br>// var keytype=$('#keyType').combobox('getValue'); <br>var keytype = 'Goods_Name'; <br>var keywords = val; <br>$('#selectgoods-grid').datagrid('reload', { keytype: keytype, keywords: keywords }); <br>} <br>function saveSelectGoods() { <br>var ids = []; <br>var rows = $('#selectgoods-grid').datagrid('getSelections'); <br>for (var i = 0; i < rows.length; i++) { <br>ids.push(rows[i].Identifier); <br>} <br>var selectsupplier = '<%=ViewData["supplier"] %>'; <br>$.post('/SupplierGoods/SaveSelect', { supplier: selectsupplier, checks: ids.join(',') }, function(data) { <br>if (data) { <br>$('#goodslist-grid').datagrid('reload'); <br>$('#goodsInfo-window').window('close'); <br>} else { <br>alert('保存失败!'); <br>} <br>}, 'json'); <br>} <br></script> <br><div style="width:100%; height:100%"> <br><table id="selectgoods-grid" class="easyui-datagrid" fit="true" toolbar="#tlb_selectgoods_search" pagination="true" <br>rownumbers="true" fitColumns="true" idField="Identifier"> <br><thead> <br><tr> <br><th field="ck" checkbox="true"></th> <br><th field="Identifier" hidden="true" width="0" editor="text">Id</th> <br><th field="Goods_Name" width="100" editor="{type:'validatebox',options:{required:true}}">商品名称</th> <br><th field="Chemistry" width="100" editor="{type:'validatebox',options:{required:true}}">化学指标</th> <br><th field="Physical" width="100" editor="{type:'validatebox',options:{required:true}}">物理指标</th> <br><th field="Partner_Name" width="50" editor="{type:'validatebox',options:{required:true}}">合作状态</th> <br></tr> <br></thead> <br></table> <br><div id="tlb_selectgoods_search"> <br>商品名称:<input name="keywords" id="selectgoods-keywords" class="easyui-searchbox" /><a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:saveSelectGoods()">保存</a> <br></div> <br></div> <br> <br>ASP.NET MVC的Controller代码如下: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>/// <summary> <br>/// 多选商品添加 <br>/// </summary> <br>/// <param name="supplier">供货商ID</param> <br>/// <returns></returns> <br>public ActionResult SelectGoods(string supplier) <br>{ <br>ViewData["supplier"] = supplier; <br>return View(); <br>} <br>/// <summary> <br>/// 保存批量添加的产品信息 <br>/// </summary> <br>/// <param name="checks">选中的商品ID</param> <br>/// <param name="supplier">供货商名称</param> <br>/// <returns></returns> <br>public JsonResult SaveSelect(string checks, string supplier) <br>{ <br>JsonResult result = new JsonResult(); <br>result.Data = false; <br>try <br>{ <br>if (String.IsNullOrEmpty(supplier)) <br>return result; <br>SupplierGoods goods = new SupplierGoods(); <br>goods.Identifier = 0; <br>//拼装xml <br>String ids=Communion.StringHelper.BuildXmlID(checks); <br>goods.Goods_ID = -1;//标示批量插入 <br>goods.Note = ids; <br>goods.Month_Output = Convert.ToDouble(String.IsNullOrEmpty(this.ReadFromRequest("Month_Output")) ? "0" : this.ReadFromRequest("Month_Output")); <br>goods.Supplier_ID = Convert.ToInt32(supplier); <br>goods.Create_Date = DateTime.Now; <br>goods.Customers = this.ReadFromRequest("Customers"); <br>goods.Equipment = this.ReadFromRequest("Equipment"); <br>goods.Detail_Params = this.ReadFromRequest("Detail_Params"); <br>goods.IsDefault = Convert.ToInt32(String.IsNullOrEmpty(this.ReadFromRequest("IsDefault")) ? "0" : this.ReadFromRequest("IsDefault")); <br>Business business = new BusinessLogic(); <br>int id = business.Save<SupplierGoods>(goods); <br>if (goods.Identifier == 0) <br>{ <br>goods.Identifier = id; <br>} <br>result.Data = true; <br>return result; <br>} <br>catch (Exception e) <br>{ <br>return result; <br>} <br>} <br> <br>存储过程利用xml变量对传入的xml类型的ID集合进行批量添加保存到数据库中,存储过程代码如下: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>ALTER PROCEDURE [dbo].[View_SupplierGoodsCreate] <br>@Identifier int, <br>@Supplier_ID int, <br>@Goods_ID int, <br>@isDefault int, <br>@Create_Date datetime, <br>@Month_Output float(8), <br>@Goods_Name nvarchar(400)=NULL, <br>@Physical nvarchar(400)=NULL, <br>@Chemistry nvarchar(400)=NULL, <br>@Customers nvarchar(400)=NULL, <br>@Equipment nvarchar(400)=NULL, <br>@Note nvarchar(MAX)=NULL, <br>@Detail_Params nvarchar(400)=NULL <br>AS <br>IF @Goods_ID=-1 <br>BEGIN <br>--批量插入商品 <br>DECLARE @xml xml <br>SET @xml=@Note <br>INSERT INTO Supplier_Goods(Supplier_ID,Goods_ID,Create_Date,Month_Output,Customers,Equipment,Note,isdefault,Detail_Params) <br>SELECT @Supplier_ID,identifier,@Create_Date,0,null,null,null,0,null <br>FROM Base_Goods <br>WHERE <br>Identifier in (Select <br>T.ID.value('.', 'int') As ID <br>From <br>@xml.nodes('/XML/ID') as T(ID)) and Identifier not in (select goods_id from Supplier_Goods where Supplier_ID=@Supplier_ID) <br>SET @Identifier=@Goods_ID <br>END <br></li><li> </li><li> </li></ol></pre></li></ol></pre>

人气教程排行