public ActionResult EditArticle(int Id, ArticleDataBindingModel model) { ViewBag.ListCategory = GetCategory(); if (ModelState.IsValid) { var editArticle = ApiEndPoint.GenerateUpdateAricleUrl(Id); try { using (HttpClient httpClient = new HttpClient()) { var jsonString = JsonConvert.SerializeObject(model); var data = new StringContent(jsonString, Encoding.UTF8, "application/json"); Debug.WriteLine(data); HttpResponseMessage result = httpClient.PutAsync(editArticle, data).Result; if (!result.IsSuccessStatusCode) { TempData["Status"] = "Fail to save "; //request failed Debug.WriteLine("[failed]"); return(View(model)); } var jsonResult = result.Content.ReadAsStringAsync().Result; if (jsonResult == null) { Debug.WriteLine("[null response]"); return(View(model)); } var articleResult = JsonConvert.DeserializeObject <Article>(jsonResult); TempData["Status"] = "[Success save:]" + articleResult.Id; Debug.WriteLine("[success]"); return(RedirectToAction("ArticleDetail", new { id = articleResult.Id })); } } catch (Exception err) { TempData["Status"] = "Fail connect to api"; Debug.WriteLine(err.Message); Debug.WriteLine("[failed]"); return(View(model)); } } else { return(View(model)); } }
public ActionResult BrowserAnArticle(int Id, ArticleDataBindingModel article) { var updateArticleApiUrl = ApiEndPoint.GenerateUpdateAricleUrl(Id); try { using (HttpClient httpClient = new HttpClient()) { article.Status = 1; var jsonString = JsonConvert.SerializeObject(article); var data = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage result = httpClient.PutAsync(updateArticleApiUrl, data).Result; if (!result.IsSuccessStatusCode) { //request failed Debug.WriteLine("failed"); TempData["UpdateArticleStatus"] = "Updated failed"; return(RedirectToAction("ArticleDetail", new { id = Id })); } var jsonResult = result.Content.ReadAsStringAsync().Result; var articleResult = JsonConvert.DeserializeObject <Article>(jsonResult); TempData["UpdateArticleStatus"] = "Updated failed"; return(RedirectToAction("ListPendingArticle")); } } catch (Exception err) { TempData["UpdateArticleStatus"] = "Cannot connect to API"; Debug.WriteLine(err.Message); return(RedirectToAction("ArticleDetail", new { id = Id })); } }