• Entityframework批量删除


    UI层

    1 <a href="#" onclick="DelData(0);return false;" id="a_del" class="easyui-linkbutton" iconcls="icon-cancel">删除</a>   

    UI中js:

     1  //删除按钮事件
     2          function DelData(id) {
     3              $.messager.confirm('提示', '确认删除?', function (r) {
     4                  if (r) {
     5                      var selected = "";
     6                      if (id <= 0) {
     7                          $($('#tab_list').datagrid('getSelections')).each(function () {
     8                              selected += this.ID + ",";
     9                          });
    10                          selected = selected.substr(0, selected.length - 1);
    11                          if (selected == "") {
    12                              $.messager.alert('提示', '请选择要删除的数据!', 'info');
    13                              return;
    14                          }
    15                      }
    16                      else {
    17                          selected = id;
    18                      }
    19                      $.post('/DataGrid/GetJson', { "action": "del", "cbx_select": selected }, function (data) {
    20                          $.messager.alert('提示', data, 'info', function () { $("#tab_list").datagrid("reload"); });
    21                      });
    22                  }
    23              });
    24          }

    MVC中控制器:

    1.action:

     1  [HttpPost]
     2         public ActionResult GetJson()
     3         {
     4             UserManagerServiceClient client = new UserManagerServiceClient();
     5 
     6             string action = string.Empty;
     7             if (Request.Form["action"] != "")
     8             {
     9                 action = Request.Form["action"].ToString();
    10             }
    11             switch (action)
    12             {
    13                 case "query":                                                     //第一次进入页面时查询数据
    14                     string JsonString = QueryEmployee(client);
    15                     return Content(JsonString.ToString());
    16                 case "submit":
    17                     string UpdateMsg = UpdateEmployInfo(client);
    18                     return Content(UpdateMsg);
    19                 case "queryone":
    20                     string JsonOneEmployee = QueryOneEmployee(client);
    21                     return Content(JsonOneEmployee);    
    22                 case "del" :
    23                     string DelMsg = DelEmployees(client);
    24                     return Content(DelMsg);  
    25                 default:
    26                     return Content("");
    27             }

    2.DelEmployees()方法:

      /// <summary>
            /// 删除员工信息
            /// </summary>
            /// <param name="client"></param>
            /// <returns></returns>
            private string DelEmployees(UserManagerServiceClient client)
            {
                string msg = "删除失败!";
                string selectedID = Request.Form["cbx_select"] != "" ? Request.Form["cbx_select"] : "";
                if (selectedID != "" && selectedID !="0")
                {
                    int delCount = client.DelEmployee(selectedID); // 从服务端返回的删除员工信息的个数
                    if (delCount > 0)
                    {
                        msg = string.Format("本次共删除了{0}条员工信息!", delCount);
                    }
                }
    
                return msg;
            }

    WCF服务端代码:EF中contains就 好似sql中的in

     /// <summary>
            /// 删除员工信息
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public int DelEmployee(string id)
            {
                int msg = 0;
                try
                {
                    List<string> strID = id.Split(',').ToList();
                    List<int> arrayID = strID.ConvertAll(e => int.Parse(e)); 
                    using (UserManageDB db = new UserManageDB())
                    {
    
                        using (TransactionScope transaction =new TransactionScope()  )
                        {
                            foreach (var employee in db.EmployeInfo.Where(e => arrayID.Contains(e.ID)))
                            {
                                db.EmployeInfo.Remove(employee);
                                msg++;
                            }                      
                            db.SaveChanges();
                            transaction.Complete();
                        }
                       
                       
                    }
    
                    return msg;
                }
                catch (Exception ex)
                {                
                    throw ex;
                }
            }
  • 相关阅读:
    SpringBoot框架中,使用过滤器进行加密解密操作(一)
    Oracle SQL Developer 连接数据库问题总结
    ios 自动布局
    iOS 跳转动画 改变pushViewController动画
    ios修饰符
    iOS 屏幕弹框
    iOS 单例类,
    Swift 阳历转农历,农历转公历
    Swift限制只能输入中文(粘贴无效),Swift限制输入字数
    swift判断 UILabel文字是否被折叠 是否有省略号 是否没显示全
  • 原文地址:https://www.cnblogs.com/lihongchen/p/3640613.html
Copyright © 2020-2023  润新知