private List <Error> SaveGrid(CheckListTaskVM grid1, IEnumerable <KeyValuePair <string, ModelState> > state, CheckList checkList) { List <Error> errors = new List <Error>(); // Deleted if (grid1.deleted != null) { foreach (CheckListTaskViewModel model in grid1.deleted) { var chlistTask = new ChecklistTask { Id = model.Id }; _hrUnitOfWork.CheckListRepository.Remove(chlistTask); } } // Exclude delete models from sever side validations if (ServerValidationEnabled) { var modified = Models.Utils.GetModifiedRows(state.Where(a => !a.Key.Contains("deleted"))); if (modified.Count > 0) { errors = _hrUnitOfWork.CompanyRepository.Check(new CheckParm { CompanyId = CompanyId, ObjectName = "ChecklistTasks", TableName = "ChecklistTasks", Columns = Models.Utils.GetModifiedRows(state.Where(a => !a.Key.Contains("deleted"))), Culture = Language }); if (errors.Count() > 0) { return(errors); } } } // updated records if (grid1.updated != null) { foreach (CheckListTaskViewModel model in grid1.updated) { var chlistTask = new ChecklistTask(); AutoMapper(new Models.AutoMapperParm { Destination = chlistTask, Source = model }); chlistTask.ModifiedTime = DateTime.Now; chlistTask.ModifiedUser = UserName; _hrUnitOfWork.CheckListRepository.Attach(chlistTask); _hrUnitOfWork.CheckListRepository.Entry(chlistTask).State = EntityState.Modified; } } // inserted records if (grid1.inserted != null) { foreach (CheckListTaskViewModel model in grid1.inserted) { var chlistTask = new ChecklistTask(); AutoMapper(new Models.AutoMapperParm { Destination = chlistTask, Source = model }); chlistTask.Checklist = checkList; chlistTask.CreatedTime = DateTime.Now; chlistTask.CreatedUser = UserName; _hrUnitOfWork.CheckListRepository.Add(chlistTask); } } return(errors); }
public ActionResult Details(CheckListFormViewModel model, OptionsViewModel moreInfo, CheckListTaskVM grid1) { List <Error> errors = new List <Error>(); if (ModelState.IsValid) { if (ServerValidationEnabled) { errors = _hrUnitOfWork.SiteRepository.CheckForm(new CheckParm { CompanyId = CompanyId, ObjectName = "CheckList", TableName = "CheckLists", ParentColumn = "CompanyId", Columns = Models.Utils.GetColumnViews(ModelState.Where(a => !a.Key.Contains('.'))), Culture = Language }); if (errors.Count() > 0) { foreach (var e in errors) { foreach (var errorMsg in e.errors) { ModelState.AddModelError(errorMsg.field, errorMsg.message); } } return(Json(Models.Utils.ParseFormErrors(ModelState))); } } CheckList record; //insert if (model.Id == 0) { record = new CheckList(); _hrUnitOfWork.JobRepository.AddLName(Language, record.Name, model.Name, model.LocalName); AutoMapper(new Models.AutoMapperParm { Destination = record, Source = model, ObjectName = "CheckList", Options = moreInfo, Transtype = TransType.Insert }); record.CreatedUser = UserName; record.CreatedTime = DateTime.Now; record.CompanyId = model.IsLocal ? CompanyId : (int?)null; if (record.StartDate > record.EndDate) { ModelState.AddModelError("EndDate", MsgUtils.Instance.Trls("EndDateGthanStartDate")); return(Json(Models.Utils.ParseFormErrors(ModelState))); } _hrUnitOfWork.CheckListRepository.Add(record); } //update else { record = _hrUnitOfWork.Repository <CheckList>().FirstOrDefault(a => a.Id == model.Id); _hrUnitOfWork.JobRepository.AddLName(Language, record.Name, model.Name, model.LocalName); AutoMapper(new Models.AutoMapperParm { Destination = record, Source = model, ObjectName = "CheckList", Options = moreInfo, Transtype = TransType.Update }); record.ModifiedTime = DateTime.Now; record.ModifiedUser = UserName; record.CompanyId = model.IsLocal ? CompanyId : (int?)null; if (record.StartDate > record.EndDate) { ModelState.AddModelError("EndDate", MsgUtils.Instance.Trls("EndDateGthanStartDate")); return(Json(Models.Utils.ParseFormErrors(ModelState))); } _hrUnitOfWork.CheckListRepository.Attach(record); _hrUnitOfWork.CheckListRepository.Entry(record).State = EntityState.Modified; } // Save grid1 errors = SaveGrid(grid1, ModelState.Where(a => a.Key.Contains("grid")), record); if (errors.Count > 0) { return(Json(errors.First().errors.First().message)); } errors = SaveChanges(Language); var message = "OK"; if (errors.Count > 0) { message = errors.First().errors.First().message; } return(Json(message)); } return(Json(Models.Utils.ParseFormErrors(ModelState))); }