public ActionResult AddOrEdit(mvcEmploye emp) { try { if (emp.EmployeId == 0) { ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; HttpPostedFileBase img = null; if (Request.Files.Count > 0) { if (Request.Files[0].ContentLength > 0) { img = Request.Files[0]; } } byte[] fileData = new byte[img.ContentLength]; img.InputStream.ReadAsync(fileData, 0, img.ContentLength); emp.image = fileData; HttpResponseMessage response = GlobalVariables.webapiClient.PostAsJsonAsync("Employe", emp).Result; TempData["SuccessMessage"] = "Data Saved Successfully"; return(RedirectToAction("Index")); } else { HttpPostedFileBase img = null; if (Request.Files.Count > 0) { if (Request.Files[0].ContentLength > 0) { img = Request.Files[0]; byte[] fileData = new byte[img.ContentLength]; img.InputStream.ReadAsync(fileData, 0, img.ContentLength); emp.image = fileData; } } if (emp.image == null) { HttpResponseMessage response = GlobalVariables.webapiClient.GetAsync("Employe/" + emp.EmployeId.ToString()).Result; var readTask = response.Content.ReadAsAsync <mvcEmploye>(); emp.image = readTask.Result.image; } HttpResponseMessage response1 = GlobalVariables.webapiClient.PutAsJsonAsync("Employe/" + emp.EmployeId, emp).Result; TempData["SuccessMessage"] = "Data Updated Successfully"; return(RedirectToAction("Index")); } } catch (Exception ex) { return(View(ex.InnerException)); } }
// GET: Employe public ActionResult Index() { try { ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; mvcEmploye employe = new mvcEmploye(); List <mvcEmploye> empList = new List <mvcEmploye>(); HttpResponseMessage response = GlobalVariables.webapiClient.GetAsync("Employe").Result; if (response.IsSuccessStatusCode) { var readTask = response.Content.ReadAsAsync <List <mvcEmploye> >(); readTask.Result.ForEach(emp => emp.imageString = Convert.ToBase64String(emp.image)); readTask.Wait(); empList = readTask.Result; } return(View(empList)); } catch (Exception ex) { return(View(ex.InnerException)); } }