数据批量删除_从页面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;
        }

猜你喜欢

转载自www.cnblogs.com/mjxxsc/p/10572423.html
今日推荐