public ActionResult EditStore(StoreItemVM storeItem) { var keys = Request.Form; var x = Request.Body; Store s = StoreVmToStore(storeItem); Store2 s2 = new Store2 { productID = s.productID, productColor = s.productColor, productSize = s.productSize, productPhoto = s.productPhoto, productQuantity = s.productQuantity, ID = s.ID, Product = s.Product }; string data = JsonConvert.SerializeObject(s2); StringContent reqBody = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage responseMessage = client.PutAsync($"http://shirleyomda-001-site1.etempurl.com/odata/Stores({s.ID})", reqBody).Result; if (responseMessage.IsSuccessStatusCode) { ViewBag.Message = "added"; } else { ViewBag.Message = "Error not added"; } return(RedirectToAction("Details", new RouteValueDictionary( new { controller = "Products", action = "Details", id = s.productID }))); }
//[Bind("file,ID,productID,productColor,productSize,productQuantity")] public ActionResult AddStore(StoreItemVM storeItem) { var keys = Request.Form; var x = Request.Body; //for (int i = 0; i < keys.Length; i++) //{ // Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>"); //} Store s = StoreVmToStore(storeItem); string data = JsonConvert.SerializeObject(s); StringContent reqBody = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage responseMessage = client.PostAsync("http://shirleyomda-001-site1.etempurl.com/odata/Stores", reqBody).Result; if (responseMessage.IsSuccessStatusCode) { ViewBag.Message = "added"; } else { ViewBag.Message = "Error not added"; } return(RedirectToAction("Details", new RouteValueDictionary( new { controller = "Products", action = "Details", id = s.productID }))); }
public IActionResult OnGet(int?id) { StoreItemObj = new StoreItemVM { StoreItem = new Models.Models.StoreItem(), CategoryList = _unitOfWork.Category.GetCategoryListForDropDown(), SizeList = _unitOfWork.Size.GetSizeListForDropDown() }; if (id != null) { StoreItemObj.StoreItem = _unitOfWork.StoreItem.GetFirstOrDefault(u => u.Id == id); if (StoreItemObj == null) { return(NotFound()); } } return(Page()); }
public Store StoreVmToStore(StoreItemVM storeItem) { Store store = new Store(); if (storeItem.file == null) { //store.productImage = storeItem.productImage; store.productPhoto = null; } else { if (storeItem.file.Length > 0) { string uploadsFolder = Path.Combine(this.Environment.WebRootPath, "Images"); string uniqueFileName = Guid.NewGuid().ToString() + "_" + storeItem.file.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { storeItem.file.CopyTo(fileStream); } store.productPhoto = uniqueFileName; //using (var ms = new MemoryStream()) //{ // storeItem.file.CopyTo(ms); // var fileBytes = ms.ToArray(); // store.productImage = fileBytes; //} } } store.Carts = new List <Cart>(); // store.BillProducts = new List<BillProduct>(); store.ID = storeItem.ID; store.productColor = storeItem.productColor; store.productID = storeItem.productID; store.productSize = storeItem.productSize; store.productQuantity = storeItem.productQuantity; return(store); }
public IActionResult OnPost() { string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (!ModelState.IsValid) { if (StoreItemObj.StoreItem.Id != 0) { StoreItemObj.StoreItem = _unitOfWork.StoreItem.GetFirstOrDefault(u => u.Id == StoreItemObj.StoreItem.Id); if (StoreItemObj == null) { return(NotFound()); } StoreItemObj.CategoryList = _unitOfWork.Category.GetCategoryListForDropDown(); StoreItemObj.SizeList = _unitOfWork.Size.GetSizeListForDropDown(); } else { StoreItemObj = new StoreItemVM { StoreItem = new Models.Models.StoreItem(), CategoryList = _unitOfWork.Category.GetCategoryListForDropDown(), SizeList = _unitOfWork.Size.GetSizeListForDropDown() }; } return(Page()); } if (StoreItemObj.StoreItem.Id == 0) //means a brand new menuItem { //physically upload and save image string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images\storeitems"); var extension = Path.GetExtension(files[0].FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create)) { files[0].CopyTo(fileStream); } //save the string data path StoreItemObj.StoreItem.Image = @"\images\menuitems\" + fileName + extension; _unitOfWork.StoreItem.Add(StoreItemObj.StoreItem); } else //update { var objFromDb = _unitOfWork.StoreItem.Get(StoreItemObj.StoreItem.Id); if (files.Count > 0) { //physically upload and save image string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images\storeitems"); var extension = Path.GetExtension(files[0].FileName); var imagePath = Path.Combine(webRootPath, objFromDb.Image.TrimStart('\\')); //trim off forward slash if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } using (var fileStream = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create)) { files[0].CopyTo(fileStream); } //save the string data path StoreItemObj.StoreItem.Image = @"\images\storeitems\" + fileName + extension; } else { StoreItemObj.StoreItem.Image = objFromDb.Image; } _unitOfWork.StoreItem.Update(StoreItemObj.StoreItem); } _unitOfWork.Save(); return(RedirectToPage("./Index")); }