public async Task <IActionResult> Create(CommercialViewModel 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.Commercial.StartDate; var endDate = viewModel.Commercial.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)) { Commercial commercial = viewModel.Commercial; commercial.ID = Guid.NewGuid(); commercial.PolicyID = policyId; _context.Add(commercial); 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.Commercial.ComponentID); return(View(viewModel)); }
public ActionResult GetCommercial() { IReadOnlyCollection <Commercial> commercials = new List <Commercial>(); var commercialResult = _commercialProvider.GetCommercials(); if (commercialResult.IsSuccess()) { commercials = commercialResult.GetSuccessResult(); } var model = new CommercialViewModel { Commercials = commercials }; return(PartialView("CommercialPartial", model)); }
// GET: Commercials/Create public async Task <IActionResult> Create(Guid productId, Guid clientId, Guid policyId) { Commercial commercial = new Commercial { StartDate = DateTime.Now, EndDate = DateTime.Now }; CommercialViewModel viewModel = new CommercialViewModel { ProductID = productId, ClientID = clientId, PolicyID = policyId, Commercial = commercial, ComponentList = new SelectList(await _context.Components.ToListAsync(), "ID", "Name", await _context.Components.FirstOrDefaultAsync()) }; return(View(viewModel)); }