public async Task <IActionResult> Edit(int?id, MeasurementViewModel viewModel) { if (id == null) { id = 0; } var measure = await _measurementRepository.GetMeasurementByNameAsync(viewModel.Name); if (measure != default(Measurement)) { if (id == 0) { ModelState.AddModelError("", "Measurement with this name already exist!!!"); } if ((measure.MeasurementID != id) & (id != 0)) { ModelState.AddModelError("", "Measurement with this name already exist!!!"); } } try { if (ModelState.IsValid) { var saving1 = await _measurementRepository.SaveMeasurementAsync(viewModel, (int)id); if (saving1 != null) { return(RedirectToAction("Index")); } } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(viewModel)); }