コード例 #1
0
        public async Task <CriarPaisViewModel> PostAsync(CriarPaisViewModel criarPaisViewModel)
        {
            var criarPaisViewModelJson = JsonConvert.SerializeObject(criarPaisViewModel);

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

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

            if (response.IsSuccessStatusCode)
            {
                return(criarPaisViewModel);
            }

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

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

                criarPaisViewModel.Errors = listErro;

                return(criarPaisViewModel);
            }

            return(criarPaisViewModel);
        }
コード例 #2
0
        public async Task <IActionResult> Create(CriarPaisViewModel criarPaisViewModel, IFormFile foto)
        {
            var urlFoto = UploadFotoPessoa(foto, criarPaisViewModel.Nome);

            criarPaisViewModel.UrlFoto = urlFoto;

            try
            {
                if (ModelState.IsValid == false)
                {
                    return(View(criarPaisViewModel));
                }

                var client  = new RestClient();
                var request = new RestRequest(_UriAPI + "api/Pais", DataFormat.Json);

                request.AddJsonBody(criarPaisViewModel);

                var response = await client.PostAsync <CriarPaisViewModel>(request);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
コード例 #3
0
        public async Task <CriarPaisViewModel> PostAsync(CriarPaisViewModel paisViewModel)
        {
            var paisViewModelJson = JsonConvert.SerializeObject(paisViewModel);

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

            var response = await httpClient.PostAsync("api/paises", conteudo);

            if (response.IsSuccessStatusCode)
            {
                return(paisViewModel);
            }

            return(paisViewModel);
        }
コード例 #4
0
        public ActionResult Create(CriarPaisViewModel criarPaisViewModel)
        {
            if (ModelState.IsValid)
            {
                var client = new RestClient();

                var request = new RestRequest("https://localhost:5001/api/paises", DataFormat.Json);
                var urlFoto = UploadFotoAmigo(criarPaisViewModel.Fotoarq);
                criarPaisViewModel.Bandeira = urlFoto;
                request.AddJsonBody(criarPaisViewModel);
                var response = client.Post <CriarPaisViewModel>(request);
                return(Redirect("/pais/index"));
            }
            return(BadRequest());
        }
コード例 #5
0
        public async Task <ActionResult> Create(CriarPaisViewModel criarPaisViewModel)
        {
            try
            {
                var urlFoto = UploadFotoPais(criarPaisViewModel.Foto);
                criarPaisViewModel.UrlFoto = urlFoto.Result;

                await _apiPais.PostAsync(criarPaisViewModel);

                return(Redirect("/"));
            }
            catch
            {
                return(View());
            }
        }
コード例 #6
0
        public ActionResult Create(CriarPaisViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var restClient = new RestClient();
            var request    = new RestRequest("http://localhost:5000/api/paises", DataFormat.Json);

            viewModel.Bandeira = UploadFoto(viewModel.BandeiraArq);
            request.AddJsonBody(viewModel);
            var response = restClient.Post <CriarPaisViewModel>(request);

            return(Redirect("/pais/index"));
        }
コード例 #7
0
        public async Task <ActionResult> Create(CriarPaisViewModel criarpaisViewModel)
        {
            var foto = UploadFotoPais(criarpaisViewModel.ImgFoto);

            criarpaisViewModel.Foto = foto;

            await _paisApi.PostAsync(criarpaisViewModel);

            try
            {
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }