public IActionResult Edit(int personNameId, PersonNameEditModel editModel) { // // Get data // var name = _personData.GetPersonNameWithNavigation(personNameId); if (name == null) return NotFound(); // Read to validate var person = _personData .ReadPersonWithNames( name.Person.PersonGuid); if (person == null) return NotFound(); // // View helpers // Model must be ready if validation fails // editModel.NameCount = person.Names.Count; ViewData["PersonGuid"] = person.PersonGuid; // // Validate // var form = new PersonNameModelState( ModelState, new Logic.Validate.PersonName( editModel.Prefix, editModel.First, editModel.Middle, editModel.Last, editModel.Suffix, editModel.NameWeight), person.Names, name.PersonNameId); form.HasValidValues(); if (!ModelState.IsValid) return View(editModel); form.HasUniquePersonNameToBeEdited(); if (!ModelState.IsValid) return View(editModel); form.NameWeightHasValidNumber(); if (!ModelState.IsValid) return View(editModel); //TODO check that the validation is the same as at the creation // // Edit // name.Prefix = editModel.Prefix; name.First = editModel.First; name.Middle = editModel.Middle; name.Last = editModel.Last; name.Suffix = editModel.Suffix; name.NameWeight = editModel.NameWeight; try { // Update name in form _personData.UpdatePersonName(name); // Addjust name weight at the other names _personData.AdjustAllNameWeightsButOne( person.PersonId, name.PersonNameId); } 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(editModel); } // // Update RDF // var readPerson = _personData.ReadAllPersonData(person.PersonGuid); if (readPerson != null) _rdfData.AddOrUpdatePerson(readPerson); // // Update search index // var names = new List<string>(); person = _personData .ReadPersonWithNames( name.Person.PersonGuid); foreach (var n in person.Names.OrderByDescending(x => x.NameWeight)) { var fullName = string.Join(" ", new[] { n.Prefix, n.First, n.Middle, n.Last, n.Suffix }.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray()); names.Add(fullName); } // TODO try catch to handle errors _personSearchIndex.MergeNames( person.PersonGuid.ToString(), names.ToArray()); // // Redirect // return RedirectToAction( "Details", "Person", new { personGuid = name.Person.PersonGuid }); }