public ActionResult Create(int categoryId, FormCollection form, HttpPostedFileBase fileUpload) { using (var context = new SiteContext()) { var category = context.Category.First(c => c.Id == categoryId); var product = new Product{SortOrder = 0}; TryUpdateModel(product, new[] { "Name","Date", "Title",/* "SortOrder",*/ "SeoDescription", "SeoKeywords" }); product.Description = HttpUtility.HtmlDecode(form["Description"]); product.Category = category; if (fileUpload != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); //GraphicsHelper.SaveOriginalImage(filePath, fileName, fileUpload); fileUpload.SaveAs(filePath); product.ImageSource = fileName; } context.AddToProduct(product); context.SaveChanges(); return RedirectToAction("Index", "Home", new { area = "", category = category.Name, product = product.Name }); } }
public ActionResult Edit(int id, FormCollection form) { using (var context = new SiteContext()) { var content = context.Content.First(c => c.Id == id); TryUpdateModel(content, new[] {"Title", "SeoDescription", "SeoKeywords"}); content.Text = HttpUtility.HtmlDecode(form["Text"]); context.SaveChanges(); return RedirectToAction("Index","Home",new{area=""}); } }
public ActionResult Edit(int id, FormCollection form) { using (var context = new SiteContext()) { var category = context.Category.First(c => c.Id == id); TryUpdateModel(category, new[] { "Name", "Title", "TitleToCategory", "SeoDescription", "SeoKeywords", "SortOrder" }); category.Description = HttpUtility.HtmlDecode(form["Description"]); context.SaveChanges(); return RedirectToAction("Index", "Home", new { area = "", category = category.Name }); } }
public ActionResult Delete(int id) { using (var context = new SiteContext()) { var image = context.SecretImage.First(i => i.Id == id); ImageHelper.DeleteImage(image.ImageSource); ImageHelper.DeleteImage(image.PreviewImageSource); context.DeleteObject(image); context.SaveChanges(); return RedirectToAction("SiteContent", "Home", new { id = "secretlink", area = "" }); } }
public ActionResult Delete(int id) { using (var context = new SiteContext()) { var category = context.Category.Include("Products").First(c => c.Id == id); if (!category.Products.Any()) { context.DeleteObject(category); context.SaveChanges(); } return RedirectToAction("Index", "Home", new { area = "" }); } }
public ActionResult Create(FormCollection collection, List<HttpPostedFileBase> mainImage, List<HttpPostedFileBase> previewImage) { try { using (var context = new SiteContext()) { int imagesCount = previewImage.Count(file => file != null); for (int i = 0; i < imagesCount; i++) { SecretImage si = new SecretImage(); string fileName = IOHelper.GetUniqueFileName("~/Content/Images", previewImage[i].FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); previewImage[i].SaveAs(filePath); si.PreviewImageSource = fileName; fileName = IOHelper.GetUniqueFileName("~/Content/Images", mainImage[i].FileName); filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); mainImage[i].SaveAs(filePath); si.ImageSource = fileName; context.AddToSecretImage(si); context.SaveChanges(); } return RedirectToAction("SiteContent", "Home", new { id = "secretlink", area = "" }); } } catch { return View(); } }
public ActionResult Delete(int id) { using (var context = new SiteContext()) { var product = context.Product.Include("Category").Include("ProductItems").First(c => c.Id == id); var categoryName = product.Category.Name; while (product.ProductItems.Any()) { var productItem = product.ProductItems.First(); ImageHelper.DeleteImage(productItem.ImageSource); context.DeleteObject(productItem); } ImageHelper.DeleteImage(product.ImageSource); context.DeleteObject(product); context.SaveChanges(); return RedirectToAction("Index", "Home", new { area = "", category = categoryName }); } }
public ActionResult Edit(int id, FormCollection form, HttpPostedFileBase fileUpload) { using (var context = new SiteContext()) { var product = context.Product.Include("Category").First(p => p.Id == id); TryUpdateModel(product, new[] { "Name", "Date", "Title",/* "SortOrder",*/ "SeoDescription", "SeoKeywords" }); product.Description = HttpUtility.HtmlDecode(form["Description"]); if (fileUpload != null) { if (!string.IsNullOrEmpty(product.ImageSource)) { IOHelper.DeleteFile("~/Content/Images", product.ImageSource); } string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); //GraphicsHelper.SaveOriginalImage(filePath, fileName, fileUpload); fileUpload.SaveAs(filePath); product.ImageSource = fileName; } context.SaveChanges(); return RedirectToAction("Index", "Home", new { area = "", category = product.Category.Name, product=product.Name }); } }
public ActionResult Delete(int id) { using (var context = new SiteContext()) { var pi = context.ProductItem.Include("Product").First(p => p.Id == id); var productId = pi.Product.Id; var product = context.Product.Include("Category").First(p => p.Id == productId); ImageHelper.DeleteImage(pi.ImageSource); context.DeleteObject(pi); context.SaveChanges(); return RedirectToAction("Index", "Home", new { area = "", category = product.Category.Name, product = product.Name }); } }