MVC4 batch data deletion and mvc4 batch data deletion
Effect:
JQuery code:
<Script type = "text/javascript">
$ (Document). ready (function (){
// Select all
$ ("# SelectAll"). click (function (){
$ ("Input [name = 'rkey']"). prop ("checked", this. checked );
});
// Single answer
Var subChk = $ ("input [name = 'rkey']")
SubChk. click (function (){
$ ("# SelectAll"). prop ("checked", subChk. length = subChk. filter (": checked"). length? True: false );
});
/* Batch Delete */
$ ("# DeleteBtn"). click (function (){
// Determine if at least one item is selected
Var checkedNum = $ ("input [name = 'rkey']: checked"). length;
If (checkedNum = 0 ){
Alert ("select at least one item! ");
Return;
}
// Batch select
If (confirm ("are you sure you want to delete the selected project? ")){
Var checkedList = new Array ();
$ ("Input [name = 'rkey']: checked"). each (function (){
CheckedList. push ($ (this). val ());
});
$. Ajax ({
Type: "POST ",
Url: "../Logs/DeleteMore ",
Data: {'delitems ': checkedList. toString ()},
DataType: "text ",
Success: function (result ){
Alert (result );
$ ("[Name = 'rkey']: checkbox"). attr ("checked", false );
Window. location. reload ();
}
});
}
});
});
</Script>
HTML code:
<Table class = "table-bordered table-striped table-hover">
<Tbody>
<Tr align = "center">
<Td nowrap = "nowrap">Background code:
/// <Summary>
/// Batch Delete Log Data
/// </Summary>
/// <Returns> </returns>
Public ActionResult DeleteMore ()
{
ArrayList arr = new ArrayList ();
String rkeyStr = "";
StringBuilder sb = new StringBuilder ();
If (Request ["delitems"]! = Null & Request ["delitems"]. ToString ()! = "")
{
RkeyStr = Request ["delitems"]. ToString ();
String [] rkeyArr = rkeyStr. Split (',');
Int count = 0;
For (int I = 0; I <rkeyArr. Length; I ++)
{
String sqlStr = "delete from Logs where RKEY =" + Convert. ToInt32 (rkeyArr [I]) + "";
Count = SqlHelper. ExecuteSql (sqlStr );
}
If (count> 0)
{
Log. DeleteLogs ("batch Delete log Data ");
String str = "batch deletion successful! ";
Return Content (str );
}
}
Else
{
RkeyStr = "";
String str = "batch deletion failed! ";
Return Content (str );
}
Return null;
}