public async Task <ActionResult> Edit(DepartmentUpdate.CommandModel commandModel) { var request = new DepartmentUpdate.Request(SystemPrincipal.Name, commandModel); var response = await DomainServices.DispatchAsync <DepartmentUpdate.Response>(request); if (!response.HasValidationIssues) { return(RedirectToAction("Index")); } if (response.RowVersion != null) { commandModel.RowVersion = response.RowVersion; } var instructors = await _QueryRepository.GetEntitiesAsync <Instructor>(new AsNoTrackingQueryStrategy());; ViewBag.InstructorID = new SelectList(instructors, "ID", "FullName", commandModel.InstructorID); ModelState.AddRange(response.ValidationDetails.AllValidationMessages); return(View(commandModel)); }
private static ValidationMessageCollection OnUpdateFailedFunc(IRepository repository, DbUpdateConcurrencyException dbUpdateEx, DepartmentUpdate.CommandModel commandModel, ref byte[] rowVersion) { var validationMessages = new ValidationMessageCollection(); var entry = dbUpdateEx.Entries.Single(); var databaseEntry = entry.GetDatabaseValues(); if (databaseEntry == null) { validationMessages.Add(string.Empty, "Unable to save changes. The department was deleted by another user."); return(validationMessages); } var databaseValues = (Department)databaseEntry.ToObject(); rowVersion = databaseValues.RowVersion; if (databaseValues.Name != commandModel.Name) { validationMessages.Add(nameof(commandModel.Name), "Current value: " + databaseValues.Name); } if (databaseValues.Budget != commandModel.Budget) { validationMessages.Add(nameof(commandModel.Budget), "Current value: " + string.Format("{0:c}", databaseValues.Budget)); } if (databaseValues.StartDate != commandModel.StartDate) { validationMessages.Add(nameof(commandModel.StartDate), "Current value: " + string.Format("{0:d}", databaseValues.StartDate)); } if (databaseValues.InstructorID != commandModel.InstructorID) { validationMessages.Add(nameof(commandModel.InstructorID), "Current value: " + repository.GetEntity <Instructor>(p => p.ID == databaseValues.InstructorID.Value).FullName); } validationMessages.Add(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); return(validationMessages); }