public ActionResult ChangeCategory(int?id)
        {
            if (id == null)
            {
                return(View("Error"));
            }
            var prod = db.Products
                       .Include(m => m.Category)
                       .FirstOrDefault(m => m.Id == id);

            if (prod == null)
            {
                return(View("Error"));
            }
            var possibleCats = db.Categories
                               .Where(m => db.Categories.Where(p => p.CategoryId == m.Id).FirstOrDefault() == null)
                               .ToList();

            var possibleCatNames = possibleCats
                                   .Where(m => m.StoreId == prod.Category.StoreId && m.Id != prod.CategoryId)
                                   .Select(m => m.Name)
                                   .ToList();

            var model = new ProductChangeCategoryViewModel
            {
                Id = prod.Id
            };

            ViewBag.Name    = prod.Name;
            ViewBag.Current = prod.Category.Name;
            ViewBag.Cats    = possibleCatNames;

            return(View(model));
        }
        public ActionResult ChangeCategory(ProductChangeCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                var prod = db.Products
                           .Include(m => m.Specifications)
                           .FirstOrDefault(m => m.Id == model.Id);
                if (prod == null)
                {
                    return(View("Error"));
                }

                var cat = db.Categories
                          .Include(m => m.Attributes)
                          .FirstOrDefault(m => m.Name == model.SelectedCategory);
                prod.CategoryId = cat.Id;
                prod.Category   = cat;

                db.Entry(prod).State = EntityState.Modified;

                db.SaveChanges();
                if (true)
                {
                    foreach (var spec in db.Specifications.Where(m => m.ProductId == prod.Id).ToList())
                    {
                        db.Specifications.Remove(spec);
                    }
                    var allAtrs = cat.Attributes.ToList();
                    allAtrs.Reverse();
                    var parent = db.Categories
                                 .Include(m => m.Attributes)
                                 .FirstOrDefault(m => cat.CategoryId == m.Id);

                    while (parent != null)
                    {
                        var newAtrs = parent.Attributes.ToList();
                        newAtrs.Reverse();
                        foreach (var atr in newAtrs)
                        {
                            allAtrs.Add(atr);
                        }
                        parent = db.Categories
                                 .Include(m => m.Attributes)
                                 .FirstOrDefault(m => parent.CategoryId == m.Id);
                    }
                    allAtrs.Reverse();
                    foreach (var atr in allAtrs)
                    {
                        db.Specifications.Add(new Specification
                        {
                            Name      = atr.Name,
                            Value     = atr.Default,
                            ProductId = prod.Id
                        });
                    }
                    db.SaveChanges();
                }

                return(RedirectToAction("Details", new { id = model.Id }));
            }

            return(RedirectToAction("ChangeCategory", new { id = model.Id }));
        }