public ActionResult Add(Category c, string webpage, string tmpl) { DBDataContext db = Utils.DB.GetContext(); if (ModelState.IsValid) { db.Categories.InsertOnSubmit(c); try { db.SubmitChanges(); return RedirectToAction("Index", "Category", new { controller = "Category", action = "Index", webpage = webpage, tmpl = tmpl}); } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred. Please try again in few minutes."); ErrorHandler.Report.Exception(ex, "Category/Add[HTTPPOST]"); } } ViewData["Title"] = "Add Category"; ViewData["Action"] = "Add"; return View("Manage", c); }
public ActionResult ManageDocument(Document doc, DocumentFile file, ImageFile thumb, string category, string name, string webPageId, string tmpl) { string dbPath = "", dbThumbPath = ""; DBDataContext db = Utils.DB.GetContext(); //check for category <> 0 int categoryId = 0; if (!string.IsNullOrEmpty(name)) { //add category to database Category c = new Category() { Name = name }; try { db.Categories.InsertOnSubmit(c); db.SubmitChanges(); categoryId = c.ID; } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred while adding category to the database."); ErrorHandler.Report.Exception(ex, "Body/ManageDocument[HTTPPOST]"); } } //no new category, try parsing existing one from dropdown list if (categoryId == 0) { Int32.TryParse(category, out categoryId); } //validate category if (categoryId > 0) { WebPage page = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webPageId)); if (page != null) { if (file != null) { file.ValidateForUpload(true); if (ModelState.IsValid) { string documentName = "doc-lib_" + DateTime.Now.Ticks.ToString() + "_" + new Random().Next(1, 100).ToString() + System.IO.Path.GetExtension(file.Name); if (file.Save(documentName)) { dbPath = "/" + file.SavePath; file.Cleanup(); } } } else { ModelState.AddModelError("", "Document file is not recognized"); } if (thumb != null) { thumb.ValidateForUpload(true); if (ModelState.IsValid) { string thumbName = "doc-lib_thumb_" + DateTime.Now.Ticks.ToString() + "_" + new Random().Next(1, 100).ToString(); if (thumb.Save(thumbName, new System.Drawing.Size(Config.DocLib.Thumbnail.Width, Config.DocLib.Thumbnail.Height), false)) { dbThumbPath = "/" + thumb.SavePath; thumb.Cleanup(); } } } else { ModelState.AddModelError("", "Image file is not recognized"); } if (!string.IsNullOrEmpty(dbPath) && !string.IsNullOrEmpty(dbThumbPath)) { Category c = db.Categories.SingleOrDefault(x => x.ID == categoryId); Document d = new Document() { Title = doc.Title, Source = dbPath, Thumb = dbThumbPath }; d.Category = c; page.Documents.Add(d); try { db.SubmitChanges(); return RedirectToAction("DocLib", "Body", new { controller = "Body", action = "DocLib", webpage = webPageId.ToString(), tmpl = tmpl }); } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred. Please try again in a few minutes."); ErrorHandler.Report.Exception(ex, "Body/ManageDocument[HTTPPOST]"); } } else { ModelState.AddModelError("", "There has been an issue with uploading your Image file. Please try again in few minutes."); } } else { ModelState.AddModelError("", "Page cannot be found. Please try again in few minutes."); } } else { ModelState.AddModelError("category", "You must either add new or select valid category for this document."); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbPath)); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbThumbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbThumbPath)); } ViewData["WebPageID"] = webPageId; ViewData["TemplateID"] = tmpl; return View("ManageDocument", new Document()); }
partial void UpdateCategory(Category instance);
partial void DeleteCategory(Category instance);
partial void InsertCategory(Category instance);