SoFunction
Updated on 2025-03-08

MVC using EasyUI's datagrid multiple-choice submission and saving tutorial

The datagrid component that implements EasyUI needs to be added to the checkbox column and submit the function of batch addition in the background. The page code is as follows:
Copy the codeThe code is as follows:

<script language="javascript" type="text/javascript">
$(function() {
//searchbox
$('#selectgoods-keywords').searchbox({
searcher: function(val, name) {
searchInfo(val);
}
});
//datagrid
$('#selectgoods-grid').datagrid({
url: '/Goods/List',
pageNumber: 1,
pageSize: 20,
pageList: [20, 40, 60, 80, 100]
});
//form
});
function searchInfo(val){
// var keytype=$('#keyType').combobox('getValue');
var keytype = 'Goods_Name';
var keywords = val;
$('#selectgoods-grid').datagrid('reload', { keytype: keytype, keywords: keywords });
}
function saveSelectGoods() {
var ids = [];
var rows = $('#selectgoods-grid').datagrid('getSelections');
for (var i = 0; i < ; i++) {
(rows[i].Identifier);
}
var selectsupplier = '<%=ViewData["supplier"] %>';
$.post('/SupplierGoods/SaveSelect', { supplier: selectsupplier, checks: (',') }, function(data) {
if (data) {
$('#goodslist-grid').datagrid('reload');
$('#goodsInfo-window').window('close');
} else {
alert('Save failed!');
}
}, 'json');
}
</script>
<div style="width:100%; height:100%">
<table class="easyui-datagrid" fit="true" toolbar="#tlb_selectgoods_search" pagination="true"
rownumbers="true" fitColumns="true" idField="Identifier">
<thead>
<tr>
<th field="ck" checkbox="true"></th>
<th field="Identifier" hidden="true" width="0" editor="text">Id</th>
<th field="Goods_Name" width="100" editor="{type:'validatebox',options:{required:true}}">Product name</th>
<th field="Chemistry" width="100" editor="{type:'validatebox',options:{required:true}}">Chemical indicators</th>
<th field="Physical" width="100" editor="{type:'validatebox',options:{required:true}}">Physical indicators</th>
<th field="Partner_Name" width="50" editor="{type:'validatebox',options:{required:true}}">Cooperation status</th>
</tr>
</thead>
</table>
<div >
Product name: <input name="keywords" class="easyui-searchbox" /><a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:saveSelectGoods()">Save</a>
</div>
</div>

The Controller code of MVC is as follows:
Copy the codeThe code is as follows:

/// <summary>
/// Add multiple selections
/// </summary>
/// <param name="supplier">Supplier ID</param>
/// <returns></returns>
public ActionResult SelectGoods(string supplier)
{
ViewData["supplier"] = supplier;
return View();
}
/// <summary>
/// Save batch added product information
/// </summary>
/// <param name="checks">Selected product ID</param>
/// <param name="supplier">Supplier name</param>
/// <returns></returns>
public JsonResult SaveSelect(string checks, string supplier)
{
JsonResult result = new JsonResult();
= false;
try
{
if ((supplier))
return result;
SupplierGoods goods = new SupplierGoods();
= 0;
//Assemble xml
String ids=(checks);
goods.Goods_ID = -1;//Sign up batch insertion
= ids;
goods.Month_Output = ((("Month_Output")) ? "0" : ("Month_Output"));
goods.Supplier_ID = Convert.ToInt32(supplier);
goods.Create_Date = ;
= ("Customers");
= ("Equipment");
goods.Detail_Params = ("Detail_Params");
= Convert.ToInt32((("IsDefault")) ? "0" : ("IsDefault"));
Business business = new BusinessLogic();
int id = <SupplierGoods>(goods);
if ( == 0)
{
= id;
}
= true;
return result;
}
catch (Exception e)
{
return result;
}
}

Stored procedures use xml variables to batch add and save the incoming xml type ID set to the database. The stored procedure code is as follows:
Copy the codeThe code is as follows:

ALTER PROCEDURE [dbo].[View_SupplierGoodsCreate]
@Identifier int,
@Supplier_ID int,
@Goods_ID int,
@isDefault int,
@Create_Date datetime,
@Month_Output float(8),
@Goods_Name nvarchar(400)=NULL,
@Physical nvarchar(400)=NULL,
@Chemistry nvarchar(400)=NULL,
@Customers nvarchar(400)=NULL,
@Equipment nvarchar(400)=NULL,
@Note nvarchar(MAX)=NULL,
@Detail_Params nvarchar(400)=NULL
AS
IF @Goods_ID=-1
BEGIN
--Batch insert product
DECLARE @xml xml
SET @xml=@Note
INSERT INTO Supplier_Goods(Supplier_ID,Goods_ID,Create_Date,Month_Output,Customers,Equipment,Note,isdefault,Detail_Params)
SELECT @Supplier_ID,identifier,@Create_Date,0,null,null,null,0,null
FROM Base_Goods
WHERE
Identifier in (Select
('.', 'int') As ID
From
@('/XML/ID') as T(ID)) and Identifier not in (select goods_id from Supplier_Goods where Supplier_ID=@Supplier_ID)
SET @Identifier=@Goods_ID
END