ajax刪除DB資料

來源:互聯網
上載者:User

標籤:

1.前台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     });10 11     $(‘input[name="btnDelete"]‘).click(function () {12         var checkCount = 0;13 14         $(‘input[type="checkbox"]‘).not(‘input[name="checkAll"]‘).each(function (index, element) {15             if ("checked" == $(this).attr("checked")) {16                 checkCount++;17             }18         });19 20         if (checkCount <= 0) {21             alert("請選擇您要刪除的資料!");22             return false;23         }24 25         if (confirm("確定要刪除嗎?")) {26             var data = "[";27 28             $(‘input[type="checkbox"]‘).not(‘input[name="checkAll"]‘).each(function (index, element) {29                 if ("checked" == $(this).attr("checked")) {30                     data += "{id : " + $(this).val() + "} ,";31                 }32             });33 34             if (data != "[") {35                 data = data.substring(0, data.length - 1) + "]";36 37                 $.post("/User/DeleteById", data, function success(result) {38                     //1.ajax返回單個字串內容39                     //var msg = " <font style=‘color:red‘>" + result + "</font>";40                     //if ($(‘#ajaxMsg‘).html() == ‘‘) {41                     //    $(‘#ajaxMsg‘).append(msg);42                     //}43 44                     //2.ajax返回多個字串內容45                     var msg = " <font style=‘color:red‘>刪除成功!</font>";46                     if ($(‘#ajaxMsg‘).html() == ‘‘) {47                         $(‘#ajaxMsg‘).append(msg);48                     }49 50                     $(‘input[name="checkAll"]‘).removeAttr("checked");51 52                     $(‘input[type="checkbox"]‘).not(‘input[name="checkAll"]‘).each(function (index, element) {53                         $(this).parent().parent().remove();54                     });55 56                     var ajaxResult = $.parseJSON(result);57                     $.each(ajaxResult, function (index, element) {58                         var tr = "<tr>"59                                   + "<td><input type=‘checkbox‘ value=‘" + element.ID + "‘ /></td>"60                                   + "<td><a href=‘/User/Detail/" + element.ID + "‘>" + element.UserName + "</a></td>"61                                   + "<td>" + element.Phone + "</td>"62                                   + "<td>" + element.Email + "</td>"63                                   + "<td>" + element.Address + "</td>"64                                   + "<td>" + element.CreateTime + "</td>"65                                   + "<td>" + element.UpdateTime + "</td>"66                                   + "<td><a href=‘/User/Edit/" + element.ID + "‘>修改</a></td>"67                                   + "<td><a href=‘/User/Delete/" + element.ID + "‘>刪除</a></td>"68                                   + "</tr>";69                         $(‘table‘).append(tr);70                     });71                 });72             }73         }74     });75 });
View Code

2.後台代碼:

[HttpPost]        public ActionResult DeleteById()        {            String ajaxId = Request.Params[0];            JavaScriptSerializer js = new JavaScriptSerializer();            List<AjaxReq> ajaxReqList = js.Deserialize<List<AjaxReq>>(ajaxId);            if (null != ajaxReqList && 0 < ajaxReqList.Count())            {                foreach (AjaxReq ajaxReq in ajaxReqList)                {                    UserDto user = new UserDto();                    user.ID = ajaxReq.ID;                    db.Entry<UserDto>(user).State = System.Data.EntityState.Deleted;                    db.SaveChanges();                }            }            List<UserDto> userList = db.Users.ToList();            //1.ajax返回單個字串內容            //return base.Json("刪除成功!");            //2.ajax返回多個字串內容            return base.Json(js.Serialize(userList));                    }
View Code

 

ajax刪除DB資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.