public ActionResult Add(FormCollection form, HttpPostedFileBase fileUpload, HttpPostedFileBase fileBannerUpload) { using (var context = new ContentStorage()) { var content = new Content { ContentLevel = 1 }; if (!string.IsNullOrEmpty(form["parentId"])) { int parentId = Convert.ToInt32(form["parentId"]); var parent = context.Content.Where(c => c.Id == parentId).First(); content.Parent = parent; content.ContentLevel = parent.ContentLevel + 1; } TryUpdateModel(content, new[] { "Name", "Title", "PageTitle", "SortOrder", "SeoDescription", "SeoKeywords" }); content.Text = HttpUtility.HtmlDecode(form["Text"]); if (fileUpload != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); fileUpload.SaveAs(filePath); content.ImageSource = fileName; } if (fileBannerUpload != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileBannerUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); fileBannerUpload.SaveAs(filePath); content.Banner = fileName; } context.AddToContent(content); context.SaveChanges(); return RedirectToAction("Index", "Home", new { id = content.Name, area = "" }); } }
/// <summary> /// Deprecated Method for adding a new object to the Content EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToContent(Content content) { base.AddObject("Content", content); }
/// <summary> /// Create a new Content object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="contentLevel">Initial value of the ContentLevel property.</param> /// <param name="name">Initial value of the Name property.</param> /// <param name="sortOrder">Initial value of the SortOrder property.</param> public static Content CreateContent(global::System.Int64 id, global::System.Int64 contentLevel, global::System.String name, global::System.Int32 sortOrder) { Content content = new Content(); content.Id = id; content.ContentLevel = contentLevel; content.Name = name; content.SortOrder = sortOrder; return content; }
public ActionResult Edit(Content model, FormCollection form, HttpPostedFileBase fileUpload, HttpPostedFileBase fileBannerUpload, bool deleteImage, bool deleteBannerImage) { using (var context = new ContentStorage()) { var content = context.Content.Where(c => c.Id == model.Id).First(); TryUpdateModel(content, new[] { "Name", "Title", "PageTitle", "SortOrder", "SeoDescription", "SeoKeywords", }); content.Text = HttpUtility.HtmlDecode(form["Text"]); if (deleteImage) { if(content.ImageSource!=null) { IOHelper.DeleteFile("~/Content/Images", content.ImageSource); content.ImageSource = null; } } if (deleteBannerImage) { if (content.Banner != null) { IOHelper.DeleteFile("~/Content/Images", content.Banner); content.Banner = null; } } if (fileUpload != null) { if (content.ImageSource != null) { IOHelper.DeleteFile("~/Content/Images", content.ImageSource); } string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); fileUpload.SaveAs(filePath); content.ImageSource = fileName; } if (fileBannerUpload != null) { if (content.Banner != null) { IOHelper.DeleteFile("~/Content/Images", content.Banner); } string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileBannerUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); fileBannerUpload.SaveAs(filePath); content.Banner = fileName; } context.SaveChanges(); return RedirectToAction("Index", "Home", new { id = content.Name, area = "" }); } }