Example #1
0
        public async Task <ActionResult> Create(CriarProprietarioViewModel criarProprietarioViewModel)
        {
            var urlFoto = UploadFotoProprietario(criarProprietarioViewModel.Foto);

            criarProprietarioViewModel.UrlFoto = urlFoto;

            await _proprietarioApi.PostAsync(criarProprietarioViewModel);

            try
            {
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
        public async Task <CriarProprietarioViewModel> PostAsync(CriarProprietarioViewModel criarProprietarioViewModel)
        {
            var criarProprietarioViewModelJson = JsonConvert.SerializeObject(criarProprietarioViewModel);

            var conteudo = new StringContent(criarProprietarioViewModelJson, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync("api/proprietarios", conteudo);

            if (response.IsSuccessStatusCode)
            {
                return(criarProprietarioViewModel);
            }
            else if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
            {
                var responseContent = await response.Content.ReadAsStringAsync();

                var erros = JsonConvert.DeserializeObject <List <string> >(responseContent);

                criarProprietarioViewModel.Erros = erros;
            }

            return(criarProprietarioViewModel);
        }