Ejemplo n.º 1
0
        public ActionResult Edit(int Collection_ID, int Page_ID, bool?Return_Home)
        {
            Collection collection = db.Collections.Find(Collection_ID);
            CollectionEditViewModel collViewMod = new CollectionEditViewModel(collection, Page_ID);

            if (collection == null)
            {
                return(HttpNotFound());
            }
            ViewBag.Display_Mode_ID = new SelectList(db.CollectionsDisplayModes, "Display_Mode_ID", "Display_Mode", collection.Display_Mode_ID);
            //ViewBag.Width_Mode_ID = new SelectList(db.CollectionsWidthModes, "Width_Mode_ID", "Width_Mode", collection.Width_Mode_ID);
            //ViewBag.Width_Mode_ID = new SelectList(db.WidthModes, "Width_Mode_ID", "Width_Mode", collection.Width_Mode_ID);
            return(View(collViewMod));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(CollectionEditViewModel collViewMod, bool?Return_Home)
        {
            Collection collection = db.Collections.Find(collViewMod.Collection_ID);

            collection.Collection_Title       = collViewMod.Collection_Title;
            collection.Collection_Description = collViewMod.Collection_Description;
            collection.Archived        = collViewMod.Archived;
            collection.Display_Mode_ID = collViewMod.Display_Mode_ID;
            db.SaveChanges();
            collViewMod.posts       = collection.Posts.ToList();
            ViewBag.Return_Home     = Return_Home;
            ViewBag.Display_Mode_ID = new SelectList(db.CollectionsDisplayModes, "Display_Mode_ID", "Display_Mode", collection.Display_Mode_ID);
            //ViewBag.Width_Mode_ID = new SelectList(db.CollectionsWidthModes, "Width_Mode_ID", "Width_Mode", collection.Width_Mode_ID);
            //ViewBag.Width_Mode_ID = new SelectList(db.WidthModes, "Width_Mode_ID", "Width_Mode", collection.Width_Mode_ID);
            return(View(collViewMod));
        }
        // GET: /BackOffice/Collection/Create
        public async Task <ActionResult> Create(int authorId = 0)
        {
            var model = new CollectionEditViewModel();

            var c = new Collection();

            c.Translations.Add(new CollectionTranslation
            {
                LanguageCode = LanguageDefinitions.DefaultLanguage
            });

            if (authorId != 0)
            {
                c.Authors = await db.Set <Author>().Where(a => a.Id == authorId).ToListAsync();
            }

            return(View(GenerateViewModel(c)));
        }
        public async Task <ActionResult> Create(CollectionEditViewModel model)
        {
            if (DoesCodeAlreadyExist(model.Collection))
            {
                ModelState.AddModelError("Collection.CatalogCode", CollectionStrings.Validation_CodeAlreadyExists);
            }

            if (ModelState.IsValid)
            {
                if (model.Logo != null)
                {
                    var newName = Guid.NewGuid().ToString() + ".jpg";

                    FileUploadHelper.SaveImage(model.Logo.InputStream,
                                               400, 400,
                                               Server.MapPath("~/Public/Collections/") + newName,
                                               FitMode.Crop);

                    model.Collection.LogoLocation = newName;
                }

                var authors = db.Set <Author>()
                              .Where(a => model.AuthorIds.Contains(a.Id));

                model.Collection.Authors = authors.ToList();

                db.Add(model.Collection);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            model.AvailableAuthors = db.Set <Author>()
                                     .Select(a => new SelectListItem
            {
                Value    = a.Id.ToString(),
                Text     = a.LastName + ", " + a.FirstName,
                Selected = model.AuthorIds.Contains(a.Id)
            })
                                     .ToList();

            return(View(model));
        }
        private CollectionEditViewModel GenerateViewModel(Collection c)
        {
            var vm = new CollectionEditViewModel();

            vm.Collection = c;

            var authorIds = c.Authors.Select(a => a.Id).ToList();

            vm.AvailableAuthors = db.Set <Author>()
                                  .Select(a => new SelectListItem
            {
                Value    = a.Id.ToString(),
                Text     = a.LastName + ", " + a.FirstName,
                Selected = authorIds.Contains(a.Id)
            })
                                  .ToList();

            return(vm);
        }
        public async Task <ActionResult> Edit(CollectionEditViewModel model)
        {
            if (DoesCodeAlreadyExist(model.Collection))
            {
                ModelState.AddModelError("Collection.CatalogCode", CollectionStrings.Validation_CodeAlreadyExists);
            }

            if (ModelState.IsValid)
            {
                // Force-update the collection's author list.
                await db.ForceLoadAsync(model.Collection, c => c.Authors);

                model.Collection.Authors = db.Set <Author>()
                                           .Where(a => model.AuthorIds.Contains(a.Id)).ToList();

                foreach (var t in model.Collection.Translations)
                {
                    db.UpdateTranslation(t);
                }

                // Update the logo if a new one is supplied. Don't allow property value changes if
                // the logo doesn't exist.
                if (model.Logo != null)
                {
                    var logo = db.GetValueFromDb(model.Collection, c => c.LogoLocation);

                    if (logo == null)
                    {
                        model.Collection.LogoLocation =
                            Guid.NewGuid().ToString() + ".jpg";

                        logo = model.Collection.LogoLocation;
                    }

                    FileUploadHelper.SaveImage(model.Logo.InputStream,
                                               400, 400,
                                               Server.MapPath("~/Public/Collections/") + logo,
                                               FitMode.Crop);
                }
                else
                {
                    db.ExcludeFromUpdate(model.Collection, c => new { c.LogoLocation });
                }

                db.Update(model.Collection);

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            model.AvailableAuthors = db.Set <Author>()
                                     .Select(a => new SelectListItem
            {
                Value    = a.Id.ToString(),
                Text     = a.LastName + ", " + a.FirstName,
                Selected = model.AuthorIds.Contains(a.Id)
            })
                                     .ToList();

            return(View(model));
        }