public async Task <JsonResult> SaveData(CompanyChangeViewModel companies) { if (companies.updated != null) { foreach (var item in companies.updated) { _companyService.Update(item); } } if (companies.deleted != null) { foreach (var item in companies.deleted) { _companyService.Delete(item); } } if (companies.inserted != null) { foreach (var item in companies.inserted) { _companyService.Insert(item); } } await _unitOfWork.SaveChangesAsync(); return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); }
public ActionResult SaveData(CompanyChangeViewModel companies) { if (companies.updated != null) { foreach (var updated in companies.updated) { _companyService.Update(updated); } } if (companies.deleted != null) { foreach (var deleted in companies.deleted) { _companyService.Delete(deleted); } } if (companies.inserted != null) { foreach (var inserted in companies.inserted) { _companyService.Insert(inserted); } } _unitOfWork.SaveChanges(); return(Json(new { Success = true }, JsonRequestBehavior.AllowGet)); }
public async Task <JsonResult> SaveDataAsync(CompanyChangeViewModel companies) { if (companies == null) { throw new ArgumentNullException(nameof(companies)); } if (this.ModelState.IsValid) { if (companies.updated != null) { foreach (var item in companies.updated) { this.companyService.Update(item); } } if (companies.deleted != null) { foreach (var item in companies.deleted) { this.companyService.Delete(item); } } if (companies.inserted != null) { foreach (var item in companies.inserted) { this.companyService.Insert(item); } } try { var result = await this.unitOfWork.SaveChangesAsync(); return(this.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(this.Json(new { success = false, err = errormessage }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { return(this.Json(new { success = false, err = e.GetBaseException().Message }, JsonRequestBehavior.AllowGet)); } } else { var modelStateErrors = string.Join(",", this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors.Select(n => n.ErrorMessage))); return(this.Json(new { success = false, err = modelStateErrors }, JsonRequestBehavior.AllowGet)); } }