数据批量删除_从页面js到后台数据库

   LayUI   批量选择的 ,然后操作

 batchdel: function () {
                    var checkStatus = table.checkStatus('LAY-gridview')
                        , checkData = checkStatus.data; //得到选中的数据
                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }

                    //var dataId = JSON.stringify(checkData[0].Id);
                    // layer.alert(JSON.stringify(checkData[0].Id));
                    var pStr = "";
                    for (var i = 0; i < checkData.length; i++) {  
                        pStr = pStr + JSON.stringify(checkData[i].Id)+",";
                    }  
                  pStr=  pStr.substring(0, pStr.length - 1);
                 
                
                 layer.confirm('确定删除该行数据吗?', function (index) {

                        //向服务端发送删除指令, 批量删除
                        var changeUrl = '<%= Url.Action("DeleteByIds", "UrlList",new {r=DateTime.Now.Ticks})  %>';

                        $.post(changeUrl, { ids: pStr }, function (reData) {
                            if (reData.success) {
                              
                                table.reload('LAY-gridview');
                                layer.msg('已删除');
                                 layer.close(index);
                            } else {
                                layer.msg("删除失败!");
                            }

                        });
                        
                    });

后台方法

  public JsonResult DeleteByIds()
        {
            UrlListBLL listBLL = new UrlListBLL();
            try
            {
                string idStr = Request.Form["ids"];
                bool result = false;
                string[] sNums= idStr.Split(',').ToArray();
                if (sNums.Length > 0)
                {
                    int[] ids = Array.ConvertAll(sNums, int.Parse);
                    result = listBLL.DeleteByIds(ids);
                }
                var json = new
                {
                    success = result,
                    msg = ""
                };
                return Json(json, JsonRequestBehavior.AllowGet);

            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                if (ex.InnerException != null)
                {
                    msg += ex.InnerException.Message;
                }

                throw ex;
            }
        }

批量删除

   /// <summary>
        /// 批量删除 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIds(int[]ids)
        {
            bool result = false;
            using (CoolToolDBEntities db = new CoolToolDBEntities())
            {
                foreach (int id in ids)
                {
                    UrlInfo utlInfo = new UrlInfo() { Id=id};
                    db.Entry<UrlInfo>(utlInfo).State = System.Data.EntityState.Deleted;
                }
                result = db.SaveChanges() > 0;
            }

            return result;
        }

 

posted @ 2019-03-21 16:19  兴想事成  阅读(423)  评论(2编辑  收藏  举报