public async Task <IActionResult> Edit(Guid id, AllRiskViewModel viewModel) { if (ModelState.IsValid) { AllRisk allRisk = viewModel.AllRisk; try { _context.Update(allRisk); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AllRiskExists(allRisk.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index", new { policyid = allRisk.PolicyID })); } return(View(viewModel)); }
public async Task <IActionResult> Create(AllRiskViewModel viewModel) { try { if (ModelState.IsValid) { // Check that Start Date and End Date are // within Policy Cover Start and Cover End Date Guid policyId = viewModel.PolicyID; viewModel.ErrMsg = string.Empty; var startDate = viewModel.AllRisk.StartDate; var endDate = viewModel.AllRisk.EndDate; CompareDates compareDates = new CompareDates(_context); if (compareDates.CompareStartDate(policyId, startDate) < 0) { viewModel.ErrMsg = $"Start Date cannot be earlier than Policy Cover Start Date"; ModelState.AddModelError(string.Empty, viewModel.ErrMsg); } if (compareDates.CompareEndDate(policyId, endDate) > 0) { viewModel.ErrMsg = $"End Date cannot be later than Policy Cover End Date"; ModelState.AddModelError(string.Empty, viewModel.ErrMsg); } if (string.IsNullOrEmpty(viewModel.ErrMsg)) { AllRisk allrisk = viewModel.AllRisk; allrisk.ID = Guid.NewGuid(); allrisk.PolicyID = policyId; _context.Add(allrisk); await _context.SaveChangesAsync(); return(RedirectToAction("PolicyRisks", "Policies", new { productId = viewModel.ProductID, clientId = viewModel.ClientID, policyId = viewModel.PolicyID })); } } } catch (DbUpdateException ex) { var errorMsg = ex.InnerException.Message.ToString(); viewModel.ErrMsg = errorMsg; ModelState.AddModelError(string.Empty, viewModel.ErrMsg); } viewModel.ComponentList = new SelectList(await _context.Components .AsNoTracking() .OrderBy(n => n.Name) .ToListAsync(), "ID", "Name", viewModel.AllRisk.ComponentID); return(View(viewModel)); }
// GET: AllRisks/Create public async Task <IActionResult> Create(Guid productId, Guid clientId, Guid policyId) { AllRisk allrisk = new AllRisk { StartDate = DateTime.Now, EndDate = DateTime.Now }; AllRiskViewModel viewModel = new AllRiskViewModel { ProductID = productId, ClientID = clientId, PolicyID = policyId, AllRisk = allrisk, ComponentList = new SelectList(await _context.Components .AsNoTracking() .OrderBy(n => n.Name) .ToListAsync(), "ID", "Name"), }; return(View(viewModel)); }