public ActionResult _FileUploadPartial(int postID = -1, int categoryID = -1, int eventID = -1, int locationID = -1, bool badges = false) { var fuVM = new FileUploadViewModel(); Post post = new Post(); Category category = new Category(); Event Event = new Event(); Location Location = new Location(); if (badges == true) { fuVM.Badges = true; } if (postID != -1) { post = _postRepo.FindAll().Where(p => p.ID == postID).Include(i => i.Images).Include(b => b.Badges).FirstOrDefault(); fuVM.post = post; } if (categoryID != -1) { category = _categoryRepo.FindByID(categoryID); fuVM.Category = category; } if (eventID != -1) { Event = _eventRepo.FindByID(eventID); fuVM.Event = Event; } if (locationID != -1) { Location = _locationRepo.FindByID(locationID); fuVM.Location = Location; } var uploadedFiles = new List<UploadedFile>(); var files = Directory.GetFiles(Server.MapPath("~/Content/image-uploads")); foreach (var file in files) { var uploadedFile = new UploadedFile() { Title = Path.GetFileName(file) }; uploadedFile.PathUrl = ("/Content/image-uploads/") + Path.GetFileName(file); if (_imgRepo.FindAll().Where(i => i.ImagePath == uploadedFile.PathUrl).FirstOrDefault() == null) { Image imageObj = new Image(); imageObj.ImagePath = uploadedFile.PathUrl; _imgRepo.Save(imageObj); } var fileInfo = new FileInfo(file); if (postID >= 1 && (badges == false)) { foreach (var postimage in post.Images) { if (postimage.ImagePath == uploadedFile.PathUrl) { uploadedFile.Checked = true; } } } if (postID >= 1 && (badges == true)) { foreach (var badgeImage in post.Badges) { if (badgeImage.ImagePath == uploadedFile.PathUrl) { uploadedFile.Checked = true; } } } if (categoryID >= 1) { foreach (var categoryimage in category.Images) { if (categoryimage.ImagePath == uploadedFile.PathUrl) { uploadedFile.Checked = true; } } } if (eventID >= 1) { foreach (var eventimage in Event.Images) { if (eventimage.ImagePath == uploadedFile.PathUrl) { uploadedFile.Checked = true; } } } if (locationID >= 1) { foreach (var locationimage in Location.Images) { if (locationimage.ImagePath == uploadedFile.PathUrl) { uploadedFile.Checked = true; } } } uploadedFiles.Add(uploadedFile); } fuVM.UploadedFiles = uploadedFiles; return PartialView("_FileUploadPartialView", fuVM); }
public bool SelectUpload(FileUploadViewModel fuVM, FormCollection form) { Post post = new Post(); Category category = new Category(); if (fuVM.post != null) { post = _postRepo.FindByID(fuVM.post.ID); post.Images = new List<Image>(); } if (fuVM.Category != null) { category = _categoryRepo.FindByID(fuVM.Category.ID); category.Images = new List<Image>(); } var listOfImagesPaths = form["images"]; var arrayOfImagesPaths = listOfImagesPaths.Split(','); foreach (var path in arrayOfImagesPaths) { Image image = new Image(); image.ImagePath = path; if (fuVM.post != null) { post.Images.Add(image); _postRepo.Save(post); } if (fuVM.Category != null) { category.Images.Add(image); _categoryRepo.Save(category); } } if (fuVM.post != null) { return true; } if (fuVM.Category != null) { return true; } return false; }
public ActionResult SaveCategory(Category Category, FormCollection form) { if (NotAllowedHere()) return RedirectAway(); if (ModelState.IsValid) { var theOldPost = _categoryRepo.FindByID(Category.ID); if (theOldPost == null) theOldPost = Category; if (theOldPost.Images == null) theOldPost.Images = new List<Image>(); var oldImages = theOldPost.Images.ToList(); var listOfImagesPaths = form["image"]; string[] arrayOfImagesPaths = null; if (listOfImagesPaths != null) arrayOfImagesPaths = listOfImagesPaths.Split(','); oldImages = theOldPost.Images.ToList(); if (arrayOfImagesPaths != null && arrayOfImagesPaths.Count() > 0) { foreach (var addedimg in _imgRepo.FindAll(c => arrayOfImagesPaths.Any(cat => cat == c.ImagePath.ToString()) && !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList()) theOldPost.Images.Add(addedimg); } foreach (var removedimg in oldImages.Where(c => arrayOfImagesPaths == null || !arrayOfImagesPaths.Any(cat2 => cat2 == c.ImagePath.ToString()))) theOldPost.Images.Remove(removedimg); _categoryRepo.Save(Category); // add a message to the viewbag TempData["message"] = string.Format("{0} has been saved", Category.Name); // return the user to the list return RedirectToAction("Index"); } else { // there is something wrong with the data values return View("AddCategory", Category); } }
/*----------------------------------------- * Category Controller * -------------------------------------*/ public ActionResult EditCategory(int id = 0) { if (NotAllowedHere()) return RedirectAway(); if (id != 0) { return View("AddCategory", _categoryRepo.FindAll().Where(c => c.ID == id).Include(i => i.Images).FirstOrDefault()); } else { Category category = new Category(); category.Created = DateTime.Now; return View("AddCategory", category); } }
public ActionResult SaveCategory(Category Category) { if (ModelState.IsValid) { _categoryRepo.Save(Category); // add a message to the viewbag TempData["message"] = string.Format("{0} has been saved", Category.Name); // return the user to the list return RedirectToAction("Index"); } else { // there is something wrong with the data values return View("AddCategory", Category); } }
/*----------------------------------------- * Category Controller * -------------------------------------*/ public ActionResult EditCategory(int id = 0) { if (id != 0) { return View("AddCategory", _categoryRepo.FindByID(id)); } else { Category category = new Category(); category.Created = DateTime.Now; return View("AddCategory", category); } }