public ActionResult Create(CheckListTemplateEditModel model) { try { var newModel = _checkListTemplateService.GetbyId(model.Id); var questionsIds = model.Questions != null && model.Questions.Any() ? model.Questions.Select(x => x.Id) : null; newModel.Questions.ForEach(x => { x.IsSelected = questionsIds != null && questionsIds.Contains(x.Id) ? true : false; }); model.Questions = newModel.Questions; if (ModelState.IsValid) { _checkListTemplateService.SaveTemplate(model, _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId); model.FeedbackMessage = FeedbackMessageModel.CreateSuccessMessage("Template is created successfully. You can create more templates or close this page."); return(View(model)); } return(View(model)); } catch (Exception ex) { model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("System Error:" + ex); return(View(model)); } }
public CheckListTemplateEditModel SaveTemplate(CheckListTemplateEditModel model, long organizationRoleUserId) { CheckListTemplate templateinDb = null; var template = Mapper.Map <CheckListTemplateEditModel, CheckListTemplate>(model); if (template.Id > 0) { templateinDb = _checkListTemplateRepository.GetById(template.Id); template.CreatedBy = templateinDb.CreatedBy; template.DateCreated = templateinDb.DateCreated; template.DateModified = DateTime.Now; template.ModifiedBy = organizationRoleUserId; } else { template.CreatedBy = organizationRoleUserId; template.DateCreated = DateTime.Now; template.Type = model.Type; } //var groupQuestions = _checkListTemplateRepository.GetAllGroupQuestions(); //var gpIds = model.Questions.Select(x => x.Id).ToArray(); //var checklistTemplateList = new List<CheckListGroupQuestionEditModel>(); //foreach (var groupQuestionId in gpIds) //{ // var groupQuestion = groupQuestions.Single(x => x.Id == groupQuestionId); // checklistTemplateList.Add(new CheckListGroupQuestionEditModel { IsSelected = true, Id = groupQuestion.Id,GroupId = groupQuestion.GroupId, QuestionId = groupQuestion.QuestionId }); //} //model.Questions = checklistTemplateList.ToArray(); using (var scope = new TransactionScope()) { template = _checkListTemplateRepository.Save(template, model.Questions.Where(x => x.IsSelected)); scope.Complete(); } model.Id = template.Id; return(model); }