コード例 #1
0
        public IActionResult Create(ToyViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.Toy.Producer = _producerRepository.GetProducerByID(model.Producer);
                model.Toy.Category = _categoryRepository.GetCategoryById(model.Category);

                _toyRepository.AddToy(model.Toy);

                if (model.Images != null)
                {
                    var helper = new ImageHelper(_environment);

                    foreach (var image in model.Images)
                    {
                        var imageName = helper.AddImage(image);
                        var gallery   = new Gallery()
                        {
                            FileName = imageName, Toy = model.Toy
                        };

                        _galleryRepository.AddImage(gallery);
                    }
                }

                _toyRepository.Save();
                return(RedirectToAction("Index"));
            }
            else
            {
                model.Categories = FormHelper.GetFormCategories(_context.Categories.ToArray());
                model.Producers  = FormHelper.GetFormProducers(_context.Producers.ToArray());
            }
            return(View(model));
        }