Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("SportsID,ProductName,Detail,Extension,Price,SportsBrandID,SportsCategoryID")] SportsMaterial sportsMaterial)
        {
            if (id != sportsMaterial.SportsID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(sportsMaterial);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SportsMaterialExists(sportsMaterial.SportsID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SportsCategoryID"] = new SelectList(_context.SportsCategories, "SportsCategoryID", "SportsCategoryName", sportsMaterial.SportsCategoryID);
            ViewData["SportsBrandID"]    = new SelectList(_context.SportsBrands, "SportsBrandID", "SportsBrandName", sportsMaterial.SportsBrandID);
            return(View(sportsMaterial));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("SportsID,ProductName,Detail,File,Price,SportsBrandID,SportsCategoryID")] SportsMaterial spMaterial)
        {
            using (var memoryStream = new MemoryStream())
            {
                await spMaterial.File.FormFile.CopyToAsync(memoryStream);

                string photoname = spMaterial.File.FormFile.FileName;
                spMaterial.Extension = Path.GetExtension(photoname);
                if (!".jpg.jpeg.png.gif.bmp".Contains(spMaterial.Extension.ToLower()))
                {
                    ModelState.AddModelError("File.FormFile", "Invalid Format of Image Given.");
                }
                else
                {
                    ModelState.Remove("Extension");
                }
            }
            if (ModelState.IsValid)
            {
                _context.Add(spMaterial);
                await _context.SaveChangesAsync();

                var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "SportsStationPhotos");
                if (!Directory.Exists(uploadsRootFolder))
                {
                    Directory.CreateDirectory(uploadsRootFolder);
                }
                string filename = spMaterial.SportsID + spMaterial.Extension;
                var    filePath = Path.Combine(uploadsRootFolder, filename);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await spMaterial.File.FormFile.CopyToAsync(fileStream).ConfigureAwait(false);
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SportsCategoryID"] = new SelectList(_context.SportsCategories, "SportsCategoryID", "SportsCategoryName", spMaterial.SportsCategoryID);
            ViewData["SportsBrandID"]    = new SelectList(_context.SportsBrands, "SportsBrandID", "SportsBrandName", spMaterial.SportsBrandID);
            return(View(spMaterial));
        }