public ActionResult Agregar(BotellaDto botella) { if (ModelState.IsValid) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(urlApi); var responseTask = client.GetAsync(String.Format("Botella/{0}", botella.Descripcion)); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsStringAsync(); readTask.Wait(); BotellaDto bot = JsonConvert.DeserializeObject <BotellaDto>(readTask.Result); if (bot != null) { BotellaViewModel b = GetBotellaViewModel(); ModelState.AddModelError(string.Empty, String.Format(StringEnum.GetStringValue(MensajeError.ExisteBotella), botella.Descripcion)); ViewBag.ModalAgregar = true; return(View("Index", b)); } } var myContent = JsonConvert.SerializeObject(botella); var buffer = System.Text.Encoding.UTF8.GetBytes(myContent); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); responseTask = client.PostAsync("Botella", byteContent); responseTask.Wait(); result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsStringAsync(); readTask.Wait(); botella = JsonConvert.DeserializeObject <BotellaDto>(readTask.Result); } } return(RedirectToAction("Index")); } else { BotellaViewModel b = GetBotellaViewModel(); ViewBag.ModalAgregar = true; return(View("Index", b)); } }
private BotellaViewModel GetBotellaViewModel() { BotellaViewModel b = new BotellaViewModel(); List <VinoDto> vinos = new List <VinoDto>(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(urlApi); var responseTask = client.GetAsync("Vino"); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsStringAsync(); readTask.Wait(); vinos = JsonConvert.DeserializeObject <List <VinoDto> >(readTask.Result); } } b.Botella = new BotellaDto(); b.Vinos = vinos; return(b); }
// GET: Botella public ActionResult Index() { BotellaViewModel botella = GetBotellaViewModel(); return(View(botella)); }