1. Front JS
1$ (). Ready (function () {2 3$ (' [name= ' Checkall '] '). Click (function () {4 if("checked" = = $ (' input[name= "Checkall"] '). attr ("Checked")) {5$ (' input[type= ' checkbox "] '). attr (" Checked "," checked ");6}Else {7$ (' input[type= ' checkbox "] '). Removeattr (" Checked ");8 }9 });Ten One$ (' input[name= ' Btndelete "]). Click (function () { A varCheckcount = 0; - -$ (' input[type= ' checkbox "] '). Not (' Input[name=" Checkall "] ').function(index, Element) { the if("checked" = = $ ( This). attr ("Checked")) { -checkcount++; - } - }); + - if(Checkcount <= 0) { +Alert ("Please select the data you want to delete!") "); A return false; at } - - if(Confirm ("OK to delete?") ")) { - vardata = "["; - -$ (' input[type= ' checkbox "] '). Not (' Input[name=" Checkall "] ').function(index, Element) { in if("checked" = = $ ( This). attr ("Checked")) { -Data + = "{id:" + $ ( This). Val () + "},"; to } + }); - the if(Data! = "[") { *data = data.substring (0, Data.length-1) + "]"; $ Panax Notoginseng$.post ("/user/deletebyid", data,functionSuccess (Result) { - //1.ajax Returns a single string content the //var msg = "<font style= ' color:red ' >" + result + "</font>"; + //if ($ (' #ajaxMsg '). html () = = ") { A //$ (' #ajaxMsg '). Append (msg); the //} + - //2.ajax returns multiple string contents $ varmsg = "<font style= ' color:red ' > deleted successfully! </font> "; $ if($ (' #ajaxMsg '). html () = = ") { -$ (' #ajaxMsg '). Append (msg); - } the -$ (' input[name= ' Checkall "]). Removeattr (" Checked ");Wuyi the$ (' input[type= ' checkbox "] '). Not (' Input[name=" Checkall "] ').function(index, Element) { -$( This). Parent (). Parent (). remove (); Wu }); - About varAjaxresult =$.parsejson (result); $$.each (Ajaxresult,function(index, Element) { - varTR = "<tr>" -+ "<td><input type= ' checkbox ' value= '" + element.id + "'/></td>" -+ "<td><a href= '/user/detail/" + element.id + "' >" + element. UserName + "</a></td>" A+ "<td>" + element. Phone + "</td>" ++ "<td>" + element. Email + "</td>" the+ "<td>" + element. Address + "</td>" -+ "<td>" + element. Createtime + "</td>" $+ "<td>" + element. UpdateTime + "</td>" the+ "<td><a href= '/user/edit/" + element.id + "' > Modify </a></td>" the+ "<td><a href= '/user/delete/" + element.id + "' > Delete </a></td>" the+ "</tr>"; the$ (' table '). Append (tr); - }); in }); the } the } About }); the});
View Code
2. Background code:
[HttpPost] PublicActionResult Deletebyid () {String Ajaxid= request.params[0]; JavaScriptSerializer JS=NewJavaScriptSerializer (); List<AjaxReq> ajaxreqlist = js. Deserialize<list<ajaxreq>>(AJAXID); if(NULL! = Ajaxreqlist &&0<Ajaxreqlist.count ()) { foreach(Ajaxreq ajaxreqinchajaxreqlist) {userdto User=Newuserdto (); User.ID=ajaxreq.id; Db. Entry<UserDto> (user). State =System.Data.EntityState.Deleted; Db. SaveChanges (); }} List<UserDto> userlist =db. Users.tolist (); //1.ajax Returns a single string content//return base. Json ("Delete succeeded!") "); //2.ajax returns multiple string contents return Base. Json (JS. Serialize (userlist)); }
View Code
AJAX Delete DB data