Пример #1
0
        public override void OnNavigatedFrom(Dictionary <string, object> viewModelState, bool suspending)
        {
            base.OnNavigatedFrom(viewModelState, suspending);

            if (viewModelState != null)
            {
                AddEntityStateValue("entityErrorsCollection", SelectedEntity.GetAllErrors(), viewModelState);
            }
        }
Пример #2
0
        // When Update button is pressed
        private async void OnUpdateEntity()
        {
            string errorMessage = string.Empty;
            bool   isCreating   = false;

            SelectedEntity.ValidateProperties();
            var updateErrors = SelectedEntity.GetAllErrors().Values.SelectMany(pc => pc).ToList();

            if (updateErrors.Count == 0)
            {
                try {
                    LoadingData = true;
                    if (SelectedEntity.Id == 0)
                    {
                        isCreating = true;
                        CrudResult = await _entityRepository.CreateEntityAsync(SelectedEntity);

                        SelectedEntity = JsonConvert.DeserializeObject <List <Entity> >(CrudResult.Content.ToString()).FirstOrDefault <Entity>();
                    }
                    else
                    {
                        CrudResult = await _entityRepository.UpdateEntityAsync(SelectedEntity);
                    }
                }
                catch (ModelValidationException mvex) {
                    // there were server-side validation errors
                    DisplayEntityErrorMessages(mvex.ValidationResult);
                }
                catch (HttpRequestException ex) {
                    ErrorMessageTitle = isCreating ? ErrorMessagesHelper.CreateAsyncFailedError : ErrorMessagesHelper.UpdateAsyncFailedError;
                    ErrorMessage      = ex.Message;
                }
                finally {
                    LoadingData = false;
                    RunAllCanExecute();
                }

                if (ErrorMessage != null && ErrorMessage != string.Empty)
                {
                    MessageDialog messageDialog = new MessageDialog(ErrorMessage, ErrorMessageTitle);
                    await messageDialog.ShowAsync();

                    _navService.GoBack();
                }
            }
            else
            {
                RunAllCanExecute();
            }
        }