コード例 #1
0
        public async Task <IActionResult> Delete(DefaultModel model)
        {
            int delete = 0;

            if (string.IsNullOrEmpty(model.ArrID))
            {
                SetMessageError("Dữ liệu trống");
                return(RedirectToAction("index"));
            }
            else
            {
                var arr      = model.ArrID.Split(',').ToList();
                int arrCount = arr != null ? arr.Count : 0;
                for (int i = 0; i < arrCount; i++)
                {
                    string ID   = arr[i];
                    var    item = _service.GetByID(ID);
                    if (item != null)
                    {
                        var listChild = _service.CreateQuery().Find(o => o.ParentID == item.ID).ToList();
                        _service.Remove(item.ID);
                        if (listChild != null)
                        {
                            await _service.RemoveRangeAsync(listChild.Select(o => o.ID).ToList());
                        }
                        delete++;
                    }
                }
                if (delete > 0)
                {
                    SetMessageSuccess("Đã xóa " + delete + " đối tượng");
                    return(RedirectToAction("Index"));
                }
                else
                {
                    SetMessageWarning("Không có đổi tượng nào bị xóa");
                    return(RedirectToAction("Index"));
                }
            }
        }