public ActionResult Delete(int id) { Content content = db.Content.Single(c => c.Id == id); List<ContentImages> __images = db.ContentImages.Where(u => u.IsDefault.Equals(false) && u.IdContent.Equals(id)).OrderBy(u => u.Id).ToList(); ContentMedia __media = new ContentMedia(); try { __media.image = db.ContentImages.Where(u => u.IsDefault.Equals(true) && u.IdContent.Equals(id)).FirstOrDefault().URL; } catch { __media.image = string.Empty; } try { __media.img1 = __images[0].URL; } catch { __media.img1 = string.Empty; } try { __media.img2 = __images[1].URL; } catch { __media.img2 = string.Empty; } try { __media.img3 = __images[2].URL; } catch { __media.img3 = string.Empty; } try { __media.img4 = __images[3].URL; } catch { __media.img4 = string.Empty; } try { __media.pdf = db.ContentFiles.Where(u => u.IdContent.Equals(id)).FirstOrDefault().URL; } catch { __media.pdf = string.Empty; } ViewBag.Media = __media; return View(content); }
public ActionResult Edit(Content content, IEnumerable<HttpPostedFileBase> images, HttpPostedFileBase pdf, HttpPostedFileBase featured, ContentMedia MetaData) { if (ModelState.IsValid) { content.TimestampEdit = DateTime.Now; db.Content.Attach(content); db.ObjectStateManager.ChangeObjectState(content, EntityState.Modified); db.SaveChanges(); if (featured != null) { var filename = Guid.NewGuid(); var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".jpg"; featured.SaveAs(path); ContentImages __newimage = new ContentImages() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".jpg", IsDefault = true }; var __todelete = db.ContentImages.Where(u => u.URL.Contains(MetaData.image)).FirstOrDefault(); if (__todelete != null) { db.ContentImages.DeleteObject(__todelete); db.SaveChanges(); } db.ContentImages.AddObject(__newimage); db.SaveChanges(); } int i = 1; foreach (var item in images) { if (item != null) { var filename = Guid.NewGuid(); var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".jpg"; item.SaveAs(path); ContentImages __newimage = new ContentImages() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".jpg", IsDefault = false }; string namefile = string.Empty; switch (i) { case 1: namefile = MetaData.img1; break; case 2: namefile = MetaData.img2; break; case 3: namefile = MetaData.img3; break; case 4: namefile = MetaData.img4; break; default: namefile = MetaData.img1; break; } var __todelete = db.ContentImages.Where(u => u.URL.Contains(namefile)).FirstOrDefault(); if (__todelete != null) { db.ContentImages.DeleteObject(__todelete); db.SaveChanges(); } db.ContentImages.AddObject(__newimage); db.SaveChanges(); } i = i + 1; } if (pdf != null) { var filename = Guid.NewGuid(); var path = Server.MapPath("~/Content/Upload/" + content.IdContentType + "/" + content.Id) + @"\" + filename.ToString().Substring(0, 8) + ".pdf"; pdf.SaveAs(path); ContentFiles __newPDF = new ContentFiles() { FileGUID = filename, IdContent = content.Id, URL = "/Content/Upload/" + content.IdContentType + "/" + content.Id + "/" + filename.ToString().Substring(0, 8) + ".pdf", IsDefault = true }; var __todelete = db.ContentFiles.Where(u => u.URL.Contains(MetaData.pdf)).FirstOrDefault(); if (__todelete != null) { db.ContentFiles.DeleteObject(__todelete); db.SaveChanges(); } db.ContentFiles.AddObject(__newPDF); db.SaveChanges(); } return RedirectToAction("Index", new { Id = content.IdContentType }); } ViewBag.IdContentType = new SelectList(db.ContentType, "Id", "Name", content.IdContentType); ViewBag.IdCountry = new SelectList(db.Country, "Id", "Country1", content.IdCountry); return View(content); }