public IActionResult InsertOrUpdate(TypeTraining typeTrains, int id) { try { var json = JsonConvert.SerializeObject(typeTrains); var buffer = System.Text.Encoding.UTF8.GetBytes(json); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); //client.DefaultRequestHeaders.Add("Authorization", HttpContext.Session.GetString("token")); if (typeTrains.Id == 0) { var result = client.PostAsync("", byteContent).Result; return(Json(result)); } else if (typeTrains.Id == id) { var result = client.PutAsync("" + id, byteContent).Result; return(Json(result)); } return(Json(404)); } catch (Exception ex) { throw ex; } }
private TypeTrainingEnum GetSelectedTypeTraining(GroupBox groupBox) { foreach (var item in groupBox.Controls) { if (item is RadioButton radioButton && radioButton.Checked) { return(TypeTraining.GetTypeTrainingEnum(radioButton.Text)); } } return(TypeTrainingEnum.InvalidType); }
public IActionResult GetById(int Id) { TypeTraining typeTrains = null; //client.DefaultRequestHeaders.Add("Authorization", HttpContext.Session.GetString("token")); var resTask = client.GetAsync("" + Id); resTask.Wait(); var result = resTask.Result; if (result.IsSuccessStatusCode) { var json = JsonConvert.DeserializeObject(result.Content.ReadAsStringAsync().Result).ToString(); typeTrains = JsonConvert.DeserializeObject <TypeTraining>(json); } else { ModelState.AddModelError(string.Empty, "Server Error."); } return(Json(typeTrains)); }