public ActionResult Edit(EditForSaleViewModel forsale) { if (ModelState.IsValid) { var f = db.ForSale.Find(forsale.Id); f.Price = forsale.Price; f.Seller = forsale.Seller; f.Title = forsale.Title; f.Trading = forsale.Trading; f.Description = forsale.Description; if (forsale.Picture != null && forsale.Picture.ContentLength > 0) { var extension = Path.GetExtension(forsale.Picture.FileName).ToLowerInvariant().Substring(1); if (!ConfigurationManager.AppSettings["AllowedImageExtensions"].Contains(extension)) { ModelState.AddModelError("PosterImage", "Filtypen ej tillåten"); return View(forsale); } } if (forsale.Picture != null && forsale.Picture.ContentLength > 0) { var extension = Path.GetExtension(forsale.Picture.FileName).ToLowerInvariant(); var old = Path.Combine(Server.MapPath(String.Concat("~", f.PicturePath))); if (System.IO.File.Exists(old)) { System.IO.File.Delete(old); } var filename = String.Concat(f.Id, "-ForSalePicture", extension); var path = Path.Combine(Server.MapPath("~/Uploads/ForSale"), filename); forsale.Picture.SaveAs(path); f.PicturePath = String.Concat("/Uploads/ForSale/", filename); } db.Entry(f).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(forsale); }
public ActionResult Edit(int? id) { var Controll = db.ForSale.Where(f => f.Id == id).Select(f => f); if (Controll.Count() > 0) { ForSale forsale = db.ForSale.Find(id); if(User.Identity.Name == forsale.Seller.Username){ var model = new EditForSaleViewModel { Id = forsale.Id, Title = forsale.Title, Description = forsale.Description, Price = forsale.Price, Trading = forsale.Trading, Seller = forsale.Seller, PicturePath = forsale.PicturePath, }; return View(model); } else { return RedirectToAction("Index"); } } else { return RedirectToAction("Index"); } }