public ActionResult Index(FormCollection fc, HttpPostedFileBase file) { PnpProducts tbl = new PnpProducts(); var allowedExtensions = new[] { ".Jpg", ".png", ".jpg", "jpeg" }; tbl.ProductName = fc["productName"].ToString(); tbl.ProductImage = file.ToString(); //getting complete url //tbl.Name = fc["Name"].ToString(); var fileName = Path.GetFileName(file.FileName); //getting only file name(ex-ganesh.jpg) var ext = Path.GetExtension(file.FileName); //getting the extension(ex-.jpg) if (allowedExtensions.Contains(ext)) //check what type of extension { string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension string myfile = name + "_" + tbl.ProductID + ext; //appending the name with id // store the file inside ~/project folder(Img) var path = Path.Combine(Server.MapPath("~/Img"), myfile); tbl.ProductImage = path; db.Products.Add(tbl); db.SaveChanges(); file.SaveAs(path); } else { ViewBag.message = "Please choose only Image file"; } return(View()); }
public ActionResult DeleteConfirmed(int id) { PnpProducts products = db.Products.Find(id); db.Products.Remove(products); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "productID,productName,productImage,productDropPercent,productDesc,productDateEndPromo,productPrice")] PnpProducts products) { if (ModelState.IsValid) { db.Entry(products).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(products)); }
public ActionResult Create([Bind(Include = "productID,productName,productImage,productDropPercent,productDesc,productDateEndPromo,productPrice")] PnpProducts products) { if (ModelState.IsValid) { db.Products.Add(products); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(products)); }
// GET: Products/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PnpProducts products = db.Products.Find(id); if (products == null) { return(HttpNotFound()); } return(View(products)); }