private data.GroupComment GetById(int?id) { data.GroupComment aux = new data.GroupComment(); using (var cl = new HttpClient()) { cl.BaseAddress = new Uri(baseurl); cl.DefaultRequestHeaders.Clear(); cl.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); //HttpResponseMessage res = await cl.GetAsync("api/Pais/5?"+id); HttpResponseMessage res = cl.GetAsync("api/GroupComment/" + id).Result; if (res.IsSuccessStatusCode) { var auxres = res.Content.ReadAsStringAsync().Result; aux = JsonConvert.DeserializeObject <data.GroupComment>(auxres); } } return(aux); }
public async Task <IActionResult> Edit(int id, [Bind("GroupCommentId,CommentText,GroupUpdateId,CommentDate")] data.GroupComment groupComment) { if (id != groupComment.GroupCommentId) { return(NotFound()); } if (ModelState.IsValid) { try { using (var cl = new HttpClient()) { cl.BaseAddress = new Uri(baseurl); var content = JsonConvert.SerializeObject(groupComment); var buffer = System.Text.Encoding.UTF8.GetBytes(content); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var postTask = cl.PutAsync("api/GroupComment/" + id, byteContent).Result; if (postTask.IsSuccessStatusCode) { return(RedirectToAction("Index")); } } } catch (Exception) { var aux2 = GetById(id); if (aux2 == null) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["GroupUpdateId"] = new SelectList(GetAllGroupUpdates(), "GroupUpdateId", "GroupUpdateId", groupComment.GroupUpdateId); return(View(groupComment)); }
public async Task <IActionResult> Create([Bind("GroupCommentId,CommentText,GroupUpdateId,CommentDate")] data.GroupComment groupComment) { if (ModelState.IsValid) { using (var cl = new HttpClient()) { cl.BaseAddress = new Uri(baseurl); var content = JsonConvert.SerializeObject(groupComment); var buffer = System.Text.Encoding.UTF8.GetBytes(content); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var postTask = cl.PostAsync("api/GroupComment", byteContent).Result; if (postTask.IsSuccessStatusCode) { return(RedirectToAction("Index")); } } } ViewData["GroupUpdateId"] = new SelectList(GetAllGroupUpdates(), "GroupUpdateId", "GroupUpdateId", groupComment.GroupUpdateId); return(View(groupComment)); }