Пример #1
0
        public IActionResult Index()
        {
            if (Convert.ToInt32(TempData["edit"]) == 1)
            {
                ViewBag.edit = true;
            }
            else if (Convert.ToInt32(TempData["delete"]) == 1)
            {
                ViewBag.delete = true;
            }
            else if (Convert.ToInt32(TempData["create"]) == 1)
            {
                ViewBag.create = true;
            }

            if (orm == 1)
            {
                return(View(qdb.retProductType()));
            }
            else
            {
                return(View(_db.ProductTypes.ToList()));
            }
        }
        public async Task <IActionResult> CreatePost()

        {
            if (!ModelState.IsValid)
            {
                List <Special_Tags> specialtags  = null;
                List <ProductTypes> producttypes = null;
                if (orm == 1)
                {
                    specialtags  = qdb.retSpecialTag();
                    producttypes = qdb.retProductType();
                }
                else
                {
                    specialtags  = _db.SpecialTags.ToList();
                    producttypes = _db.ProductTypes.ToList();
                }
                ProductsVm.SpecialTags  = specialtags;
                ProductsVm.ProductTypes = producttypes;
                return(View("Create", ProductsVm));
            }

            Products obj = null;

            if (orm == 1)
            {
                obj    = ProductsVm.Products;
                obj.Id = qdb.inProduct(ProductsVm.Products);
                //obj = qdb.retProduct(ProductsVm.Products);
            }
            else
            {
                _db.Products.Add(ProductsVm.Products);
                await _db.SaveChangesAsync();

                obj = await _db.Products.FindAsync(ProductsVm.Products.Id);
            }



            string path = _hostingEnvironment.WebRootPath;

            var files = HttpContext.Request.Form.Files;


            //if the user uploads image :
            if (files.Count != 0)
            {
                string extension = Path.GetExtension(files[0].FileName);
                string newName   = path + @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + extension;
                using (FileStream fs = new FileStream(newName, FileMode.Create))
                {
                    files[0].CopyTo(fs);
                }

                obj.Image = @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + extension;
            }
            //if user doesn't upload any image :
            else
            {
                string newName = path + @"\" + SD.ImageFolder + @"\" + SD.DefaultProductImage;
                System.IO.File.Copy(newName, path + @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + ".jpg");
                obj.Image = @"\" + SD.ImageFolder + @"\" + ProductsVm.Products.Id + ".jpg";
            }

            if (orm == 1)
            {
                qdb.upProduct(obj);
            }
            else
            {
                await _db.SaveChangesAsync();
            }

            TempData["create"] = 1;
            return(RedirectToAction(nameof(Index)));
        }