public async Task <JsonResult> SaveData(WorkChangeViewModel works) { if (works.updated != null) { foreach (var item in works.updated) { _workService.Update(item); } } if (works.deleted != null) { foreach (var item in works.deleted) { _workService.Delete(item); } } if (works.inserted != null) { foreach (var item in works.inserted) { _workService.Insert(item); } } await _unitOfWork.SaveChangesAsync(); return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); }
public ActionResult SaveData(WorkChangeViewModel works) { if (works.updated != null) { foreach (var updated in works.updated) { _workService.Update(updated); } } if (works.deleted != null) { foreach (var deleted in works.deleted) { _workService.Delete(deleted); } } if (works.inserted != null) { foreach (var inserted in works.inserted) { _workService.Insert(inserted); } } _unitOfWork.SaveChanges(); return(Json(new { Success = true }, JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> SaveDataAsync(WorkChangeViewModel works) { if (works == null) { throw new ArgumentNullException(nameof(works)); } if (ModelState.IsValid) { if (works.updated != null) { foreach (var item in works.updated) { this.workService.Update(item); } } if (works.deleted != null) { foreach (var item in works.deleted) { this.workService.Delete(item); } } if (works.inserted != null) { foreach (var item in works.inserted) { this.workService.Insert(item); } } try{ var result = await this.unitOfWork.SaveChangesAsync(); return(Json(new { success = true, result = result }, JsonRequestBehavior.AllowGet)); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { var errormessage = string.Join(",", e.EntityValidationErrors.Select(x => x.ValidationErrors.FirstOrDefault()?.PropertyName + ":" + x.ValidationErrors.FirstOrDefault()?.ErrorMessage)); return(Json(new { success = false, err = errormessage }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { return(Json(new { success = false, err = e.GetBaseException().Message }, JsonRequestBehavior.AllowGet)); } } else { var modelStateErrors = string.Join(",", ModelState.Keys.SelectMany(key => ModelState[key].Errors.Select(n => n.ErrorMessage))); return(Json(new { success = false, err = modelStateErrors }, JsonRequestBehavior.AllowGet)); } }