Exemplo n.º 1
0
        public async Task <IActionResult> Create(Product product)
        {
            product.Images = new List <ProductImage>();

            for (int i = 0; i < product.file.Length; i++)
            {
                var    ext      = Path.GetExtension(product.file[i].FileName);
                string purePath = $"{Guid.NewGuid()}{ext}";

                string fileName = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads", purePath);

                using (var fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
                {
                    product.file[i].CopyTo(fs);
                }
                product.Images.Add(new ProductImage
                {
                    Path   = purePath,
                    IsMain = i == product.fileSelectedIndex
                });
            }

            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", product.CategoryId);
            return(View(product));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,Title,Body,Image,PostDate")] Post post, IFormFile myFile)
        {
            if (myFile == null)
            {
                ModelState.AddModelError("Image", "Fayl uygun deyil");
            }
            if (ModelState.IsValid)
            {
                string extension = Path.GetExtension(myFile.FileName);
                post.Image = $"{DateTime.Now:yyyyMMddHHmmss} - {Guid.NewGuid()}{extension}";

                string phisicalPath = Path.Combine(env.ContentRootPath,
                                                   "wwwroot",
                                                   "uploads",
                                                   "images",
                                                   post.Image);

                using (FileStream fs = new FileStream(phisicalPath, FileMode.Create, FileAccess.Write))
                {
                    await myFile.CopyToAsync(fs);
                }
                db.Add(post);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(post));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Name,DeletedDate")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Email")] Subscribe subscribe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(subscribe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(subscribe));
        }