public ActionResult Create(Product product, HttpPostedFileBase Images1, HttpPostedFileBase Images2, HttpPostedFileBase Images3) { HttpContext.Server.ScriptTimeout = 1000; if (ModelState.IsValid) { User user = new User(); user = db.Users.Where(x => x.Username == User.Identity.Name).FirstOrDefault(); product.UserId = user.UserId; product.Views = 123; product.Created = DateTime.Now; if (Images1 != null) { string name = System.IO.Path.GetFileName(Images1.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload/"), name); Images1.SaveAs(path); product.Images1 = Images1.FileName; } if (Images2 != null) { string name = System.IO.Path.GetFileName(Images2.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload/"), name); Images2.SaveAs(path); product.Images2 = Images2.FileName; } if (Images3 != null) { string name = System.IO.Path.GetFileName(Images3.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload/"), name); Images3.SaveAs(path); product.Images3 = Images3.FileName; } var productid = db.Products.Where(p => p.ProductId == product.ProductId).Count(); if (productid > 0) { ModelState.AddModelError("", "* Product Id Userd"); } else { db.Products.Add(product); db.SaveChanges(); return RedirectToAction("Index"); } } return View(product); }
public ActionResult Edit(Product product, HttpPostedFileBase Images1, HttpPostedFileBase Images2, HttpPostedFileBase Images3) { HttpContext.Server.ScriptTimeout = 1000; if (ModelState.IsValid) { product.Modified = DateTime.Now; if (Images1 != null) { string name = System.IO.Path.GetFileName(Images1.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload/"), name); Images1.SaveAs(path); product.Images1 = Images1.FileName; } if (Images2 != null) { string name = System.IO.Path.GetFileName(Images2.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload/"), name); Images2.SaveAs(path); product.Images2 = Images2.FileName; } if (Images3 != null) { string name = System.IO.Path.GetFileName(Images3.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Upload/"), name); Images3.SaveAs(path); product.Images3 = Images3.FileName; } db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(product); }