public int Add(Image file) { int id = 0; if (file.ImageFile != null) { string fileName = Path.GetFileNameWithoutExtension(file.ImageFile.FileName); string extension = Path.GetExtension(file.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("tijarat") + extension; file.ImagePath = "~/Images/Product/" + fileName; fileName = Path.Combine(Server.MapPath("~/Images/Product/"), fileName); file.ImageFile.SaveAs(fileName); using (Smart_POS2Image db = new Smart_POS2Image()) { db.Images.Add(file); db.SaveChanges(); id = db.Images.Max(image => image.ImageID); } ModelState.Clear(); } else { TempData["AlertMessage"] = "Image Path is Null "; } // after successfully uploading redirect the user return(id); }
public ActionResult AddOrEdit(mvcProductModel product, Image file) { //int shopId = shop.GetShop(); //product.ShopId = shopId; if (file.ImageFile != null) { string fileName = Path.GetFileNameWithoutExtension(file.ImageFile.FileName); string extension = Path.GetExtension(file.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("tijarat") + extension; file.ImagePath = "/Images/Product/" + fileName; fileName = Path.Combine(Server.MapPath("~/Images/Product/"), fileName); file.ImageFile.SaveAs(fileName); using (Smart_POS2Image db = new Smart_POS2Image()) { db.Images.Add(file); db.SaveChanges(); product.ImageID = db.Images.Max(image => image.ImageID); } ModelState.Clear(); } else { TempData["AlertMessage"] = "Image Path is Null "; } if (product.ProdId == 0) { HttpResponseMessage request = GlobalHttp.WebApiClient.PostAsJsonAsync("Products", product).Result; TempData["SuccessMessage"] = product.ProdName + " Record Added Succfully!!"; } else { HttpResponseMessage response = GlobalHttp.WebApiClient.PutAsJsonAsync("Products/" + product.ProdId, product).Result; TempData["SuccessMessage"] = product.ProdName + " Record Updated Succfully!!"; } return(RedirectToAction("Index")); }