public ActionResult Create(tbl_Items tbl_items, HttpPostedFileBase file) { if (ModelState.IsValid) { string filesPath = "", full_path = ""; if (file != null) { char DirSeparator = System.IO.Path.DirectorySeparatorChar; filesPath = ConfigurationManager.AppSettings["ItemImages"]; full_path = Server.MapPath(filesPath).Replace("Item", ""); tbl_items.Images = FileUpload.UploadFile(file, full_path); } db.tbl_Items.Add(tbl_items); db.SaveChanges(); if (file != null) { string filename = tbl_items.ItemID + "_" + file.FileName.Replace(" ", "_").Replace("-", "_"); tbl_items.Images = FileUpload.UploadFile(file, filename, full_path); db.Entry(tbl_items).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction("Index"); } ViewBag.ItemGroupID = new SelectList(db.tbl_ItemGroup, "ItemGroupID", "ItemGroupName", tbl_items.ItemGroupID); return View(tbl_items); }
public ActionResult Edit(tbl_Items tbl_items, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null) { char DirSeparator = System.IO.Path.DirectorySeparatorChar; string FilesPath = ConfigurationManager.AppSettings["ItemImages"]; string full_path = Server.MapPath(FilesPath).Replace("Item", "").Replace("Edit", ""); if (tbl_items.Images + "" != "") FileUpload.DeleteFile(tbl_items.Images, full_path); tbl_items.Images = FileUpload.UploadFile(file, full_path); } db.Entry(tbl_items).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ItemGroupID = new SelectList(db.tbl_ItemGroup, "ItemGroupID", "ItemGroupName", tbl_items.ItemGroupID); return View(tbl_items); }