예제 #1
0
        public async Task <IActionResult> Edit(int id, Product product, Microsoft.AspNetCore.Http.IFormFile imageFile)
        {
            if (id != product.ID)
            {
                return(NotFound());
            }

            string newFile = System.IO.Path.Combine(_env.WebRootPath,
                                                    "images", imageFile.FileName);

            System.IO.FileInfo newFileInfo = new System.IO.FileInfo(newFile);

            using (System.IO.FileStream fs = newFileInfo.Create())
            {
                await imageFile.CopyToAsync(fs);

                fs.Close();
            }

            product.Image = "/images/" + imageFile.FileName;

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }