ASP.NET MVC使用EasyUI的datagrid多選提交保存教程
更新時(shí)間:2011年12月11日 22:41:48 作者:
ASP.NET MVC使用EasyUI的datagrid多選提交保存教程,需要的朋友可以參考下。
需要實(shí)現(xiàn)EasyUI的datagrid組件加入選擇checkbox列,并提交后臺(tái)批量添加的功能,頁(yè)面代碼如下:
<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 < rows.length; i++) {
ids.push(rows[i].Identifier);
}
var selectsupplier = '<%=ViewData["supplier"] %>';
$.post('/SupplierGoods/SaveSelect', { supplier: selectsupplier, checks: ids.join(',') }, function(data) {
if (data) {
$('#goodslist-grid').datagrid('reload');
$('#goodsInfo-window').window('close');
} else {
alert('保存失?。?);
}
}, 'json');
}
</script>
<div style="width:100%; height:100%">
<table id="selectgoods-grid" 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}}">商品名稱</th>
<th field="Chemistry" width="100" editor="{type:'validatebox',options:{required:true}}">化學(xué)指標(biāo)</th>
<th field="Physical" width="100" editor="{type:'validatebox',options:{required:true}}">物理指標(biāo)</th>
<th field="Partner_Name" width="50" editor="{type:'validatebox',options:{required:true}}">合作狀態(tài)</th>
</tr>
</thead>
</table>
<div id="tlb_selectgoods_search">
商品名稱:<input name="keywords" id="selectgoods-keywords" class="easyui-searchbox" /><a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:saveSelectGoods()">保存</a>
</div>
</div>
ASP.NET MVC的Controller代碼如下:
/// <summary>
/// 多選商品添加
/// </summary>
/// <param name="supplier">供貨商ID</param>
/// <returns></returns>
public ActionResult SelectGoods(string supplier)
{
ViewData["supplier"] = supplier;
return View();
}
/// <summary>
/// 保存批量添加的產(chǎn)品信息
/// </summary>
/// <param name="checks">選中的商品ID</param>
/// <param name="supplier">供貨商名稱</param>
/// <returns></returns>
public JsonResult SaveSelect(string checks, string supplier)
{
JsonResult result = new JsonResult();
result.Data = false;
try
{
if (String.IsNullOrEmpty(supplier))
return result;
SupplierGoods goods = new SupplierGoods();
goods.Identifier = 0;
//拼裝xml
String ids=Communion.StringHelper.BuildXmlID(checks);
goods.Goods_ID = -1;//標(biāo)示批量插入
goods.Note = ids;
goods.Month_Output = Convert.ToDouble(String.IsNullOrEmpty(this.ReadFromRequest("Month_Output")) ? "0" : this.ReadFromRequest("Month_Output"));
goods.Supplier_ID = Convert.ToInt32(supplier);
goods.Create_Date = DateTime.Now;
goods.Customers = this.ReadFromRequest("Customers");
goods.Equipment = this.ReadFromRequest("Equipment");
goods.Detail_Params = this.ReadFromRequest("Detail_Params");
goods.IsDefault = Convert.ToInt32(String.IsNullOrEmpty(this.ReadFromRequest("IsDefault")) ? "0" : this.ReadFromRequest("IsDefault"));
Business business = new BusinessLogic();
int id = business.Save<SupplierGoods>(goods);
if (goods.Identifier == 0)
{
goods.Identifier = id;
}
result.Data = true;
return result;
}
catch (Exception e)
{
return result;
}
}
存儲(chǔ)過(guò)程利用xml變量對(duì)傳入的xml類型的ID集合進(jìn)行批量添加保存到數(shù)據(jù)庫(kù)中,存儲(chǔ)過(guò)程代碼如下:
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
--批量插入商品
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
T.ID.value('.', 'int') As ID
From
@xml.nodes('/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
復(fù)制代碼 代碼如下:
<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 < rows.length; i++) {
ids.push(rows[i].Identifier);
}
var selectsupplier = '<%=ViewData["supplier"] %>';
$.post('/SupplierGoods/SaveSelect', { supplier: selectsupplier, checks: ids.join(',') }, function(data) {
if (data) {
$('#goodslist-grid').datagrid('reload');
$('#goodsInfo-window').window('close');
} else {
alert('保存失?。?);
}
}, 'json');
}
</script>
<div style="width:100%; height:100%">
<table id="selectgoods-grid" 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}}">商品名稱</th>
<th field="Chemistry" width="100" editor="{type:'validatebox',options:{required:true}}">化學(xué)指標(biāo)</th>
<th field="Physical" width="100" editor="{type:'validatebox',options:{required:true}}">物理指標(biāo)</th>
<th field="Partner_Name" width="50" editor="{type:'validatebox',options:{required:true}}">合作狀態(tài)</th>
</tr>
</thead>
</table>
<div id="tlb_selectgoods_search">
商品名稱:<input name="keywords" id="selectgoods-keywords" class="easyui-searchbox" /><a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:saveSelectGoods()">保存</a>
</div>
</div>
ASP.NET MVC的Controller代碼如下:
復(fù)制代碼 代碼如下:
/// <summary>
/// 多選商品添加
/// </summary>
/// <param name="supplier">供貨商ID</param>
/// <returns></returns>
public ActionResult SelectGoods(string supplier)
{
ViewData["supplier"] = supplier;
return View();
}
/// <summary>
/// 保存批量添加的產(chǎn)品信息
/// </summary>
/// <param name="checks">選中的商品ID</param>
/// <param name="supplier">供貨商名稱</param>
/// <returns></returns>
public JsonResult SaveSelect(string checks, string supplier)
{
JsonResult result = new JsonResult();
result.Data = false;
try
{
if (String.IsNullOrEmpty(supplier))
return result;
SupplierGoods goods = new SupplierGoods();
goods.Identifier = 0;
//拼裝xml
String ids=Communion.StringHelper.BuildXmlID(checks);
goods.Goods_ID = -1;//標(biāo)示批量插入
goods.Note = ids;
goods.Month_Output = Convert.ToDouble(String.IsNullOrEmpty(this.ReadFromRequest("Month_Output")) ? "0" : this.ReadFromRequest("Month_Output"));
goods.Supplier_ID = Convert.ToInt32(supplier);
goods.Create_Date = DateTime.Now;
goods.Customers = this.ReadFromRequest("Customers");
goods.Equipment = this.ReadFromRequest("Equipment");
goods.Detail_Params = this.ReadFromRequest("Detail_Params");
goods.IsDefault = Convert.ToInt32(String.IsNullOrEmpty(this.ReadFromRequest("IsDefault")) ? "0" : this.ReadFromRequest("IsDefault"));
Business business = new BusinessLogic();
int id = business.Save<SupplierGoods>(goods);
if (goods.Identifier == 0)
{
goods.Identifier = id;
}
result.Data = true;
return result;
}
catch (Exception e)
{
return result;
}
}
存儲(chǔ)過(guò)程利用xml變量對(duì)傳入的xml類型的ID集合進(jìn)行批量添加保存到數(shù)據(jù)庫(kù)中,存儲(chǔ)過(guò)程代碼如下:
復(fù)制代碼 代碼如下:
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
--批量插入商品
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
T.ID.value('.', 'int') As ID
From
@xml.nodes('/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
相關(guān)文章
asp.net(c#)實(shí)現(xiàn)從sqlserver存取二進(jìn)制圖片的代碼
有一個(gè)員工表Employee,需要保存員工照片(Photo)到數(shù)據(jù)庫(kù)(sql server)上。員工照片對(duì)應(yīng)的字段是varbinary(max),也就是要存成二進(jìn)制文件類型(這和以前討巧地存圖片文件路徑就不相同了),默認(rèn)可以為空。2011-09-09Asp.NET 生成靜態(tài)頁(yè)面并分頁(yè)的代碼
主要的原理就是替換模板里的特殊字符。2010-03-03詳解.net core webapi 前后端開發(fā)分離后的配置和部署
這篇文章主要介紹了.net core webapi 前后端開發(fā)分離后的配置和部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Visual Studio 2017 ASP.NET Core開發(fā)
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2017 ASP.NET Core開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03大型門戶網(wǎng)站實(shí)現(xiàn)的十四大技術(shù)小結(jié)
參考下大型門戶網(wǎng)站的技術(shù),大家可以盡量的備份好服務(wù)器。2010-10-10VS2015在升級(jí)到Update2之后運(yùn)行Cordova項(xiàng)目異常的解決方案
這篇文章主要介紹了VS2015在升級(jí)到Update2之后運(yùn)行Cordova項(xiàng)目異常的解決方案的相關(guān)資料,需要的朋友可以參考下2016-07-07ASP.NET全棧開發(fā)教程之在MVC中使用服務(wù)端驗(yàn)證的方法
這篇文章主要給大家介紹了關(guān)于ASP.NET全棧開發(fā)教程之在MVC中使用服務(wù)端驗(yàn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07