public int SaveArt(Art art) { int result = 0; if (art.ArtID == 0) { context.Arts.Add(art); HttpRuntime.Cache.Remove("Arts"); } else { context.Entry(art).State = EntityState.Modified; HttpRuntime.Cache.Remove("Arts"); HttpRuntime.Cache.Remove("Arts" + art.ArtID); } context.SaveChanges(); result = art.ArtID; return result; }
public ActionResult EditArt(Art art) { if (ModelState.IsValid) { art.CreationDate = DateTime.Now; foreach (string file in Request.Files) { var uploadedFile = this.Request.Files[file]; if (uploadedFile != null && uploadedFile.ContentLength > 0) { if (uploadedFile.ContentLength > 1024 * 1024 * 4) { ModelState.AddModelError("uploadError", "File size can't exceed 4 MB"); return View(art); } string fileName = uploadedFile.FileName; string[] splitFileName = fileName.Split('.'); int idFileName = artsRepository.SaveArt(art); fileName = Path.GetFileName(idFileName.ToString() + '.' + splitFileName[splitFileName.Length - 1]); var path = Path.Combine(Server.MapPath("~/Content/ArtImages/"), fileName); try { if (System.IO.File.Exists(path)) System.IO.File.Delete(path); uploadedFile.SaveAs(path); } catch (Exception) { ModelState.AddModelError("uploadError", "Can't save file to disk"); } } else { ModelState.AddModelError("uploadError", "You must upload an art image in this art section"); return View(art); } } TempData["message"] = string.Format("{0} has been saved", art.ArtTitle); return RedirectToAction("ShowArts"); } else { // there is something wrong with the data values return View(art); } }
public void DeleteArt(Art art) { context.Arts.Remove(art); context.SaveChanges(); HttpRuntime.Cache.Remove("Arts"); }