public IActionResult Edit(int?id) { if (id == null) { return(NotFound()); } var foundItem = _context.CaseReportFormOptionGroups .Where(crfopg => crfopg.ID == id) .FirstOrDefault(); var options = _context.CaseReportFormOptionChoices .Where(crogc => crogc.CaseReportFormOptionGroupId == foundItem.ID) .Select(crogc => crogc.Name) .ToArray(); if (foundItem == null) { return(NotFound()); } var viewModel = new CaseReportFormOptionGroupViewModel(); viewModel.FormAction = "Edit"; viewModel.Name = foundItem.Name; viewModel.Options = options; return(PartialView(@"~/Views/CaseReportForms/CaseReportFormOptionGroups/New.cshtml", viewModel)); }
public IActionResult New(int id) { var viewModel = new CaseReportFormOptionGroupViewModel(); viewModel.FormAction = "Create"; return(PartialView(@"~/Views/CaseReportForms/CaseReportFormOptionGroups/New.cshtml", viewModel)); }
public async Task <IActionResult> Create(CaseReportFormOptionGroupViewModel optionGroupVM) { try { if (ModelState.IsValid) { var optionGroup = new CaseReportFormOptionGroup(); optionGroup.Name = optionGroupVM.Name; optionGroup.Choices = new List <CaseReportFormOptionChoice>(); foreach (string choiceName in optionGroupVM.Options) { var optionChoice = new CaseReportFormOptionChoice(); optionChoice.Name = choiceName; optionChoice.CaseReportFormOptionGroup = optionGroup; optionGroup.Choices.Add(optionChoice); } _context.Add(optionGroup); await _context.SaveChangesAsync(); return(Json(new { result = "ok" })); } else { Hashtable errors = ModelStateHelper.Errors(ModelState); return(Json(new { success = false, errors })); } } catch (DbUpdateException) { return(null); } }
public void QueryCaseReportOptionGroups() { var results = _aspergillosisContext.CaseReportFormOptionGroups .Include(crfog => crfog.Choices) .ToList <dynamic>(); foreach (CaseReportFormOptionGroup result in results) { var viewModel = new CaseReportFormOptionGroupViewModel(); viewModel.ID = result.ID; viewModel.Name = result.Name; viewModel.Options = result.Choices.Select(c => c.Name).ToArray(); _list.Add(viewModel); } }
public IActionResult EditOptionGroupItem(int?id, CaseReportFormOptionGroupViewModel optionGroup) { if (id == null) { return(NotFound()); } var foundItem = _context.CaseReportFormOptionGroups .Include(crfog => crfog.Choices) .Where(crfc => crfc.ID == id) .FirstOrDefault(); if (foundItem == null) { return(NotFound()); } var optionsNames = optionGroup.Options; var dbOptionsChoices = foundItem.Choices.Select(c => c.Name).ToList(); var dbOptionsItems = foundItem.Choices.ToList(); var toDeleteOptions = dbOptionsChoices.Except(optionsNames); var toInsertOptions = optionsNames.Except(dbOptionsChoices); if (toDeleteOptions.Count() > 0) { var dbDeleteList = _context.CaseReportFormOptionChoices .Where(crfoc => toDeleteOptions.Contains(crfoc.Name) && crfoc.CaseReportFormOptionGroupId == id); _context.CaseReportFormOptionChoices.RemoveRange(dbDeleteList); } if (toInsertOptions.Count() > 0) { foreach (var optionName in toInsertOptions) { var option = new CaseReportFormOptionChoice() { CaseReportFormOptionGroupId = id.Value, Name = optionName }; _context.CaseReportFormOptionChoices.Add(option); } } foundItem.Name = optionGroup.Name; if (TryValidateModel(foundItem)) { try { _context.Update(foundItem); _context.SaveChanges(); } catch (DbUpdateException /* ex */) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } } else { Hashtable errors = ModelStateHelper.Errors(ModelState); return(Json(new { success = false, errors })); } return(Json(new { result = "ok" })); }