예제 #1
0
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                ProdutoViewModel novoProd = new ProdutoViewModel()
                {
                    IdCategoria         = int.Parse(collection["IdCategoria"]),
                    DataAtualizacao     = DateTime.Now.ToString(),
                    Descricao           = collection["Descricao"],
                    Nome                = collection["Nome"],
                    PrecoUnitarioCompra = decimal.Parse(collection["PrecoUnitarioCompra"]),
                    PrecoUnitarioVenda  = decimal.Parse(collection["PrecoUnitarioVenda"]),
                    Quantidade          = int.Parse(collection["Quantidade"])
                };


                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_hostService);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = client.PostAsync("api/produto/Post", new StringContent(JsonConvert.SerializeObject(novoProd))).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var             result    = response.Content.ReadAsStringAsync().Result;
                        WebApp.Response _response = JsonConvert.DeserializeObject <WebApp.Response>(result);

                        if (_response.IsSucess)
                        {
                            List <CategoriaViewModel> objetoRetorno = JsonConvert.DeserializeObject <List <CategoriaViewModel> >(_response.Data);

                            List <SelectListItem> ls = new List <SelectListItem>();

                            foreach (var item in objetoRetorno)
                            {
                                ls.Add(new SelectListItem()
                                {
                                    Text = item.Nome, Value = item.ID.ToString()
                                });
                            }
                            ViewData["category"] = ls;
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Internal server Error");
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
예제 #2
0
        // GET: Produto/Create
        public ActionResult Create()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(_hostService);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //GET Method
                HttpResponseMessage response = client.GetAsync("api/categoria/GetAll").Result;
                if (response.IsSuccessStatusCode)
                {
                    var             result    = response.Content.ReadAsStringAsync().Result;
                    WebApp.Response _response = JsonConvert.DeserializeObject <WebApp.Response>(result);

                    if (_response.IsSucess)
                    {
                        List <CategoriaViewModel> objetoRetorno = JsonConvert.DeserializeObject <List <CategoriaViewModel> >(_response.Data);

                        List <SelectListItem> ls = new List <SelectListItem>();

                        foreach (var item in objetoRetorno)
                        {
                            ls.Add(new SelectListItem()
                            {
                                Text = item.Nome, Value = item.ID.ToString()
                            });
                        }
                        ViewData["category"] = ls;
                    }
                }
                else
                {
                    //Console.WriteLine("Internal server Error");
                }
            }

            return(View());
        }