public void SaveInterChange(InterChangeFormViewModel viewModel, string action) { try { CheckSplitAmountsEntry.CheckInterChange(viewModel.TerminalId, viewModel.Id, viewModel.SplitAmount); if (viewModel.StartDate > viewModel.StopDate) { throw new Exception("Stop Date must be after Start Date. "); } var interChange = Table.SingleOrDefault(x => x.BankAccountId == viewModel.BankAccountId); if (action == "Edit") { var interChangeToEdit = Table.SingleOrDefault(x => x.Id == viewModel.Id && !x.Deleted); if (interChangeToEdit == null) { throw new Exception("InterChange not found. "); } if (interChange != null) { if (interChange.Id != interChangeToEdit.Id && !interChange.Deleted) { throw new Exception("This Terminal already has this InterChange account. "); } if (interChange.Deleted) { Table.Remove(interChange); } } Mapper.Map(viewModel, interChangeToEdit); Edit(interChangeToEdit); } else { if (interChange != null) { if (!interChange.Deleted) { throw new Exception("This Terminal already has this InterChange account. "); } Table.Remove(interChange); } var result = Mapper.Map <InterChangeFormViewModel, InterChange>(viewModel); Add(result); } } catch (Exception e) { throw new Exception(e.Message + " Could not add InterChange split amount, please check the entered values. "); } }