Example #1
0
        public async Task <IActionResult> Create(ProductCatalogyViewModel productcatalogyviewmodel, IFormFile Pimage)
        {
            if (ModelState.IsValid)
            {
                Product product = new Product();
                product.Description = productcatalogyviewmodel.Description;
                product.Tittle      = productcatalogyviewmodel.Tittle;
                product.Author      = productcatalogyviewmodel.Author;
                product.Price       = productcatalogyviewmodel.Price;
                product.Lot         = productcatalogyviewmodel.Lot;
                string filePath = "";
                if (Pimage == null)
                {
                    return(View(productcatalogyviewmodel));
                }
                if (Pimage != null)
                {
                    filePath = $"wwwroot/images/Products/{Pimage.FileName}";
                    using (var stream = System.IO.File.Create(filePath))
                    {
                        Pimage.CopyTo(stream);
                    }
                }
                ProductImage img = new ProductImage {
                    ImageFullPath = $"https://localhost:44371/images/Products/{Pimage.FileName}",
                    ProductId     = product.Id
                };
                List <ProductImage> list = new List <ProductImage>();
                list.Add(img);
                product.ProductImages = list;
                _context.Add(product);
                foreach (var item in list)
                {
                    _context.Add(item);
                }
                await _context.SaveChangesAsync();

                if (productcatalogyviewmodel.CategoryList != null)
                {
                    foreach (var item in productcatalogyviewmodel.CategoryList)
                    {
                        CategoryProduct relation = new CategoryProduct();
                        if (item.Checked == true)
                        {
                            var pr = await _context.Products.FirstOrDefaultAsync(p => p.Tittle == product.Tittle);

                            var ct = await _context.Categories.FirstOrDefaultAsync(c => c.Name == item.Name);

                            relation.IdProduct  = pr.Id;
                            relation.IdCategory = ct.Id;
                            _context.Add(relation);
                        }
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(productcatalogyviewmodel));
        }
Example #2
0
        // GET: Products/Create
        public IActionResult Create()
        {
            ProductCatalogyViewModel productcatalogyviewmodel = new ProductCatalogyViewModel();
            List <CategoryViewModel> listcategory             = new List <CategoryViewModel>();
            var category = _context.Categories;

            foreach (var item in category)
            {
                listcategory.Add(new CategoryViewModel()
                {
                    Id = item.Id, Name = item.Name, Checked = false
                });
            }

            productcatalogyviewmodel.CategoryList = listcategory;

            return(View(productcatalogyviewmodel));
        }