// GET: Customer/Edit/5 public async Task <IActionResult> Edit(int id) { try { var customerViewModel = await _customerService.GetCustomerAsync(id).ConfigureAwait(false); customerViewModel.Id = id; CECustomerViewModel cECustomerViewModel = CustomerMapper.MaptoCE(customerViewModel); var customerDiscountTypeDtos = await _customerService.GetAllCustomerDiscountType().ConfigureAwait(false); IEnumerable <CustomerDiscountTypeViewModel> customerDiscountTypeViewModels = CustomerMapper.Map(customerDiscountTypeDtos); cECustomerViewModel.CustomerDiscountTypeViewModels = customerDiscountTypeViewModels.ToList(); if (customerViewModel == null) { return(NotFound()); } return(View(cECustomerViewModel)); } catch (Exception) { ErrorViewModel model = new ErrorViewModel { RequestId = "Kunden kunne ikke rettes" }; return(View("Error", model)); } }
public async Task <IActionResult> Create([Bind("Id,Name,Phone,Address,Email,Active,Rowversion, SelectedCustomerDiscountViewModel")] CECustomerViewModel cEcustomerViewModel) { CustomerViewModel customerViewModel = CustomerMapper.Map(cEcustomerViewModel); if (ModelState.IsValid) { try { await _customerService.AddAsync(CustomerMapper.Map(customerViewModel)).ConfigureAwait(false); return(RedirectToAction(nameof(Index))); } catch (Exception) { var dbcustomer = CustomerMapper.Map((customerViewModel)); var customerDiscountTypeDtos = await _customerService.GetAllCustomerDiscountType().ConfigureAwait(false); IEnumerable <CustomerDiscountTypeViewModel> customerDiscountTypeViewModels = CustomerMapper.Map(customerDiscountTypeDtos); cEcustomerViewModel.CustomerDiscountTypeViewModels = customerDiscountTypeViewModels.ToList(); ModelState.AddModelError(string.Empty, "Email eller Telefonnummer er brugt af en anden"); return(View(cEcustomerViewModel)); } } return(View(customerViewModel)); }
// GET: Customer/Create public async Task <IActionResult> Create() { try { CECustomerViewModel cECustomerViewModel = new CECustomerViewModel(); var customerDiscountTypeDtos = await _customerService.GetAllCustomerDiscountType().ConfigureAwait(false); IEnumerable <CustomerDiscountTypeViewModel> customerDiscountTypeViewModels = CustomerMapper.Map(customerDiscountTypeDtos); cECustomerViewModel.CustomerDiscountTypeViewModels = customerDiscountTypeViewModels.ToList(); return(View(cECustomerViewModel)); } catch (Exception) { ErrorViewModel model = new ErrorViewModel { RequestId = "Kunden kunne ikke skabes" }; return(View("Error", model)); } }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Phone,Address,Email,Active,Rowversion,SelectedCustomerDiscountViewModel")] CECustomerViewModel cEcustomerViewModel) { if (id != cEcustomerViewModel.Id) { return(NotFound()); } CustomerViewModel customerViewModel = CustomerMapper.Map(cEcustomerViewModel); if (ModelState.IsValid) { try { await _customerService.UpdateAsync(id, CustomerMapper.Map(customerViewModel)).ConfigureAwait(false); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException dbu) { var dbcustomer = CustomerMapper.Map((CustomerDto)dbu.Data["dbvalue"]); if (cEcustomerViewModel.Phone != dbcustomer.Phone) { ModelState.AddModelError("Phone", "telefonnummeret er opdateret af en anden person"); } if (cEcustomerViewModel.Name != dbcustomer.Name) { ModelState.AddModelError("Name", "Navnet er opdateret af en anden person"); } if (cEcustomerViewModel.Address != dbcustomer.Address) { ModelState.AddModelError("Address", "Addressen er opdateret af en anden person"); } if (cEcustomerViewModel.Email != dbcustomer.Email) { ModelState.AddModelError("Email", "E-mailen er opdateret af en anden person"); } if (cEcustomerViewModel.CustomerDiscountType != dbcustomer.CustomerDiscountType) { ModelState.AddModelError("SelectedCustomerDiscountViewModel", "Kundetypen er opdateret af en anden person"); } var customerDiscountTypeDtos = await _customerService.GetAllCustomerDiscountType().ConfigureAwait(false); IEnumerable <CustomerDiscountTypeViewModel> customerDiscountTypeViewModels = CustomerMapper.Map(customerDiscountTypeDtos); cEcustomerViewModel.CustomerDiscountTypeViewModels = customerDiscountTypeViewModels.ToList(); cEcustomerViewModel.CustomerDiscountType = dbcustomer.CustomerDiscountType; ModelState.AddModelError(string.Empty, "Denne kunde er blevet opdateret af en anden bruger, tryk gem for at overskrive"); cEcustomerViewModel.Rowversion = dbcustomer.Rowversion; ModelState.Remove("Rowversion"); return(View("Edit", cEcustomerViewModel)); } catch (Exception) { var dbcustomer = CustomerMapper.Map((customerViewModel)); var customerDiscountTypeDtos = await _customerService.GetAllCustomerDiscountType().ConfigureAwait(false); IEnumerable <CustomerDiscountTypeViewModel> customerDiscountTypeViewModels = CustomerMapper.Map(customerDiscountTypeDtos); cEcustomerViewModel.CustomerDiscountTypeViewModels = customerDiscountTypeViewModels.ToList(); int selectedCustomerDiscountViewModel = cEcustomerViewModel.SelectedCustomerDiscountViewModel; var customerDiscountType = await _customerService.GetCustomerDiscountTypeAsync(selectedCustomerDiscountViewModel); cEcustomerViewModel.CustomerDiscountType = CustomerDiscountTypeMapper.Map(customerDiscountType); ModelState.AddModelError(string.Empty, "Email eller telefonnummer er brut af en anden"); return(View(cEcustomerViewModel)); } } return(View(cEcustomerViewModel)); }