AJAX Delete DB data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.