public async Task <ActionResult> Update(int ID) { SetSessionVariables(); ServiceResult <ResourceResponse> result = null; ResourceResponse resource = null; ResourceUpdateViewModel model = null; try { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(Common.Instance.ApiResourceControllerName); var readTask = await client.GetAsync(Common.Instance.ApiResourceGet + "/" + ID); result = await readTask.Content.ReadAsAsync <ServiceResult <ResourceResponse> >(); resource = result.Result; model = new ResourceUpdateViewModel(); model.SetViewModel(resource); } } catch { ModelState.AddModelError(string.Empty, "Server error. Please contact administrator"); } return(View(model)); }
public ActionResult Update(ResourceUpdateViewModel resourceVM) { SetSessionVariables(); resourceVM.ModifyBy = GetSessionObject().UserID; resourceVM.ModifyDateTime = DateTime.Now; string DuplicateName = ""; string OriginalName = ""; string UploadRootPath = "resource"; string uploadsFolder = "\\image\\"; string serverUploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, UploadRootPath); serverUploadsFolder += uploadsFolder; if (!Directory.Exists(serverUploadsFolder)) { Directory.CreateDirectory(serverUploadsFolder); } //Resource image uploader if (resourceVM.ResourcePlanFile != null) { OriginalName = resourceVM.ResourcePlanFile.FileName; string extension = Path.GetExtension(OriginalName); DuplicateName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension; string filePath = Path.Combine(serverUploadsFolder, DuplicateName); resourceVM.ResourcePlanFile.CopyTo(new FileStream(filePath, FileMode.Create)); resourceVM.ResourceFilePath = "\\" + UploadRootPath + uploadsFolder + DuplicateName; } Resource resource = resourceVM.GetObject(); try { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(Common.Instance.ApiResourceControllerName); var postTask = client.PostAsJsonAsync <Resource>(Common.Instance.ApiResourceUpdate, resource); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { var rs = result.Content.ReadAsAsync <ServiceResult <Resource> >().Result; } } } catch { ModelState.AddModelError(string.Empty, "Server error. Please contact Administrator"); } return(RedirectToAction("List")); }