public IActionResult OnPost(int id)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToPage("EditItem", new { id = id }));
            }

            var product = _Context.Products.FirstOrDefault(p => p.Id == id);

            product.Name                 = AIM.Name;
            product.Description          = AIM.Description;
            product.Item.Price           = AIM.Price;
            product.Item.QuantityInStock = AIM.QuantityInStock;
            _Context.SaveChanges();

            var cats = _Context.CategoriesToProducts
                       .Where(ctp => ctp.ProductId == id).ToList();

            _Context.CategoriesToProducts.RemoveRange(cats);

            var newCats = new List <CategoryToProduct>();

            foreach (var catId in AIM.Categories.Split(','))
            {
                var cid = int.Parse(catId);
                newCats.Add(new CategoryToProduct {
                    ProductId  = product.Id,
                    CategoryId = cid
                });
            }

            _Context.CategoriesToProducts.AddRange(newCats);
            _Context.SaveChanges();

            return(RedirectToPage("Index"));
        }
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var item = new Item {
                Price           = AIM.Price,
                QuantityInStock = AIM.QuantityInStock
            };

            var product = new Product {
                Name        = AIM.Name,
                Description = AIM.Description,
                Item        = item
            };

            _Context.Products.Add(product);
            _Context.SaveChanges();
            product.ItemId = product.Id;
            _Context.SaveChanges();

            // In development we are running from the current slivrr directory.
            // In Production we are running from a location inside C:\Windows\System32...
            // so we need the correct Current Directory.
            var rootPath = (_Env.IsDevelopment()) ?
                           Directory.GetCurrentDirectory() :
                           AppDomain.CurrentDomain.BaseDirectory;

            var filePath = Path.Combine(
                rootPath,
                "wwwroot",
                "images",
                product.Id + ".jpg"
                );

            if (AIM.Picture?.Length > 0)
            {
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    AIM.Picture.CopyTo(stream);
                }
            }

            var ctp = new List <CategoryToProduct>();

            foreach (var catId in AIM.Categories.Split(','))
            {
                var cid = int.Parse(catId);
                ctp.Add(new CategoryToProduct {
                    ProductId  = product.Id,
                    CategoryId = cid
                });
            }

            _Context.CategoriesToProducts.AddRange(ctp);
            _Context.SaveChanges();

            return(RedirectToPage("Index"));
        }