public IActionResult Edit(int? id, EditCategoryViewModel viewModel)
        {
            if (id == null)
                return new NotFoundResult();

            var ordinals = new Dictionary<int, int>();
            var isBig = new Dictionary<int, bool>();
            int index = 0;
            foreach (var pictureId in viewModel.GetPictureIds())
            {
                isBig[pictureId] = viewModel.GetPictureIsBigs()[index];
                ordinals[pictureId] = index++;
            }

            foreach (var picture in _context.Categories
                    .Include(c => c.Pictures)
                    .Single(c => c.Id == id)
                    .Pictures)
                picture.CategoryId = null;

            foreach (var picture in _context.Pictures
                    .Where(p => viewModel.GetPictureIds().Contains(p.Id)))
            {
                picture.CategoryId = id;
                picture.Ordinal = ordinals[picture.Id];
                picture.IsFriendly = isBig[picture.Id];
            }

            var category = _context.Categories.Single(c => c.Id == id);
            category.Name = viewModel.Name;
            category.Caption = viewModel.Caption;
            category.ThumbnailId = viewModel.ThumbnailId;
            category.ParentCategoryId = viewModel.SelectedParentCategory;

            _context.SaveChanges();

            return RedirectToAction("Edit");
        }
        public IActionResult Edit(int? id)
        {
            if (id == null)
                return new NotFoundResult();

            var category = _context.Categories
                .Single(c => c.Id == id);

            category.Pictures = _context.Pictures
                .Where(p => p.CategoryId == category.Id)
                .OrderBy(p => p.Ordinal)
                .ToList();

            var model = new EditCategoryViewModel()
            {
                Name = category.Name,
                Caption = category.Caption,
                ThumbnailId = category.ThumbnailId,
                Pictures = category.Pictures.ToList(),
                Categories = _context.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(),
                SelectedParentCategory = category.ParentCategoryId.GetValueOrDefault()
            };

            var thumbnails = _context.Categories.Select(c => c.Thumbnail).ToList();

            model.UnsortedPictures = _context.Pictures
                .Where(p => p.CategoryId == null)
                .Where(p => !thumbnails.Contains(p))
                .ToList();

            if (model.ThumbnailId != null)
                model.ThumbnailUrl = _context.Pictures
                    .SingleOrDefault(p => p.Id == model.ThumbnailId)?.Filename;

            return View(model);
        }