public ActionResult Create(int categoryId, ProductWithCategoriesViewModel model)
 {
     if (_itemsRepository.GetCategory(categoryId) == null)
     {
         ModelState.AddModelError("", "You selected an invalid category!");
     }
     if (ModelState.IsValid)
     {
         model.ProductModel.CategoryId = categoryId;
         Product newProduct = MapModelToProduct(model.ProductModel);
         _itemsRepository.AddProduct(newProduct);
         _itemsRepository.Save();
         return RedirectToAction("Index");
     }
     //Make sure the categories are loaded
     if (model.AvailableCategories == null)
     {
         model.AvailableCategories = GetAvailableCategories();
     }
     //Return to fix the errors
     return View(model);
 }
        public ActionResult Edit(int categoryId, ProductWithCategoriesViewModel model)
        {
            if (_itemsRepository.GetCategory(categoryId) == null)
            {
                ModelState.AddModelError("", "You selected an invalid category!");
            }
            if (ModelState.IsValid)
            {
                model.ProductModel.CategoryId = categoryId;
                Product updatedProduct = MapModelToProduct(model.ProductModel);
                _itemsRepository.UpdateProduct(updatedProduct);
                _itemsRepository.Save();
                return RedirectToAction("Index");
            }
            //Make sure the categories are loaded
            if (model.AvailableCategories == null)
            {
                model.AvailableCategories = GetAvailableCategories();
            }

            ////Make sure the category is constructed in case someone changed it
            //if (model.ProductModel.Category == null)
            //{
            //    Category productCat =  _itemsRepository.GetProduct(model.ProductModel.Id).Category;
            //    model.ProductModel.Category = new CategoryViewModel() { Id = productCat.Id, Name = productCat.Name };
            //}
            //Return to fix the errors
            return View(model);
        }
 public ActionResult Create()
 {
     ProductWithCategoriesViewModel model = new ProductWithCategoriesViewModel();
     model.AvailableCategories = GetAvailableCategories();
     return View(model);
 }
        public ActionResult Edit(int id)
        {
            ProductWithCategoriesViewModel model = new ProductWithCategoriesViewModel();
            var product = _itemsRepository.GetProduct(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            model.ProductModel = MapProductToModel(product);
            model.AvailableCategories = GetAvailableCategories();
            return View(model);
        }