Example #1
0
        public async Task <ActionResult> SaveData(CategoryChangeViewModel categories)
        {
            if (categories.updated != null)
            {
                foreach (var item in categories.updated)
                {
                    _categoryService.Update(item);
                }
            }
            if (categories.deleted != null)
            {
                foreach (var item in categories.deleted)
                {
                    _categoryService.Delete(item);
                }
            }
            if (categories.inserted != null)
            {
                foreach (var item in categories.inserted)
                {
                    _categoryService.Insert(item);
                }
            }
            await _unitOfWork.SaveChangesAsync();

            return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public async Task <JsonResult> SaveDataAsync(CategoryChangeViewModel categories)
        {
            if (categories == null)
            {
                throw new ArgumentNullException(nameof(categories));
            }
            if (ModelState.IsValid)
            {
                if (categories.updated != null)
                {
                    foreach (var item in categories.updated)
                    {
                        this.categoryService.Update(item);
                    }
                }
                if (categories.deleted != null)
                {
                    foreach (var item in categories.deleted)
                    {
                        this.categoryService.Delete(item);
                    }
                }
                if (categories.inserted != null)
                {
                    foreach (var item in categories.inserted)
                    {
                        this.categoryService.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));
            }
        }