Пример #1
0
        public IActionResult Save(PersonDto model)
        {
            PersonDto entity = null;

            UpdateAuditInformation(model);
            if (model.Id > 0)
            {
                entity = _customerApiClient.GetPerson(model.GlobalIdentityKey.Value.ToString()).Result;
                if (entity == null)
                {
                    ModelState.AddModelError("RowVersion", "The person is deleted by someone else. Please refresh the page and try again.");
                }
                else if (entity.RowVersion.ConvertByteArrayToString() != model.RowVersion.ConvertByteArrayToString())
                {
                    ModelState.AddModelError("RowVersion", "The person is updated by someone else. Please refresh the page and try again.");
                }
                else
                {
                    //Note: Not updating the Person entity only, as the API updated all the associated entity.
                    entity.Surname                 = model.Surname;
                    entity.Forename                = model.Forename;
                    entity.DateOfBirth             = model.DateOfBirth;
                    entity.GenderId                = model.GenderId;
                    entity.PreferredLanguageId     = model.PreferredLanguageId;
                    entity.NationalityTypeId       = model.NationalityTypeId;
                    entity.NationalInsuranceNumber = model.NationalInsuranceNumber;
                    entity.ContactTypeId           = model.ContactTypeId;
                    entity.RelationshipId          = model.RelationshipId;
                    entity.EthnicityId             = model.EthnicityId;
                    entity.TitleId                 = model.TitleId;
                }
            }
            else
            {
                model.GlobalIdentityKey = Guid.NewGuid();
            }

            if (!ModelState.IsValid)
            {
                GetStateSettings(ViewState.Write);
                PopulateDropdownList(model);
                if (Request.IsAjaxRequest())
                {
                    return(PartialView("_Details", model));
                }
                return(PartialView("Edit", model));
            }
            model = model.Id > 0
                ? _customerApiClient.PutPerson(model.Id, model).Result
                : _customerApiClient.PostPerson(model).Result;

            if (!string.IsNullOrWhiteSpace(model.ErrorMessage))
            {
                PopulateDropdownList(model);
                if (Request.IsAjaxRequest())
                {
                    return(PartialView("_Details", model));
                }
                return(PartialView("Edit", model));
            }
            return(Json(new { success = string.IsNullOrWhiteSpace(model.ErrorMessage), rowVersion = model.RowVersion, message = model.SuccessMessage }));
        }