Ejemplo n.º 1
0
        public ActionResult Add(Gallery gallery, ICollection<SelectListItem> categories)
        {
            if (ModelState.IsValid)
            {
                //int[] selectedCategoryIDs = CheckBoxListExtension.GetSelectedValues<int>("CategoriesSelected");
                //if (selectedCategoryIDs.Count<int>() > 0)
                //{
                //    List<Category> selectedCategories = unitOfWork.CategoryRepository.Get(x => selectedCategoryIDs.Contains<int>(x.CategoryID)).ToList<Category>();
                //    gallery.Categories = selectedCategories;
                //}
                int[] selectedCategoryIDs = categories.Where<SelectListItem>(x => x.Selected).Select<SelectListItem, int>(x => Convert.ToInt32(x.Value)).ToArray<int>();
                List<Category> categoriesToAdd = unitOfWork.CategoryRepository.Get(x => selectedCategoryIDs.Contains<int>(x.CategoryID)).ToList<Category>();
                gallery.Categories.Clear();
                foreach (var categoty  in categoriesToAdd)
                {
                    gallery.Categories.Add(categoty);
                }

                unitOfWork.GalleryRepository.Insert(gallery);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }

            ViewBag.MediaManagerCategory = mediaManagerCategory.GetSelectListOfCategories();
            return View(gallery);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Gallery gallery, ICollection<SelectListItem> categories)
        {
            if (ModelState.IsValid)
            {
                Gallery galleryToUpdate = unitOfWork.GalleryRepository.Get(x => x.GalleryID == gallery.GalleryID, includeProperties: "Categories").FirstOrDefault<Gallery>();
                galleryToUpdate.GalleryID = gallery.GalleryID;
                galleryToUpdate.Title = gallery.Title;
                galleryToUpdate.Description = gallery.Description;
                galleryToUpdate.Header = gallery.Header;
                galleryToUpdate.Footer = gallery.Footer;
                galleryToUpdate.ContentTitle = gallery.ContentTitle;
                galleryToUpdate.ContentDescription = gallery.ContentDescription;

                int[] selectedCategoryIDs = categories.Where<SelectListItem>(x => x.Selected).Select<SelectListItem, int>(x => Convert.ToInt32(x.Value)).ToArray<int>();
                List<Category> categoriesToAdd = unitOfWork.CategoryRepository.Get(x => selectedCategoryIDs.Contains<int>(x.CategoryID)).ToList<Category>();

                //int[] selectedCategoryIDs = CheckBoxListExtension.GetSelectedValues<int>("CategoriesSelected");
                //List<Category> selectedCategories = unitOfWork.CategoryRepository.Get(x => selectedCategoryIDs.Contains<int>(x.CategoryID)).ToList<Category>();
                //foreach (var item in selectedCategories)
                //{
                //    gallery.Categories.Add(item);
                //}
                galleryToUpdate.Categories.Clear();
                foreach (var categoty in categoriesToAdd)
                {
                    galleryToUpdate.Categories.Add(categoty);
                }

                unitOfWork.GalleryRepository.Update(galleryToUpdate);
                //unitOfWork.GalleryRepository.Update(gallery);
                //unitOfWork.GalleryRepository.context.Entry(gallery).Collection<Category>(x => x.Categories).EntityEntry.State = System.Data.Entity.EntityState.Added;
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            return View(gallery);
        }