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