예제 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Name,Phone,Email")] Supplier supplier)
        {
            if (id != supplier.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(supplier);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SupplierExists(supplier.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(supplier));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ID,Name")] Category category)
        {
            if (id != category.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(category));
        }
예제 #3
0
        public async Task <IActionResult> Edit(int id, BagViewModel viewModel)
        {
            if (id != viewModel.Bag.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                viewModel.Bag.ImagePath = "";

                if (viewModel.ImageFile != null)
                {
                    var file = viewModel.ImageFile;

                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); // FileName returns "fileName.ext"(with double quotes)

                    if (fileName.EndsWith(".jpg"))                                                                  // Important for security
                    {
                        var filePath = Path.Combine(_hostingEnvironment.WebRootPath, "uploads", fileName);

                        using (var fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                        }

                        viewModel.Bag.ImagePath = fileName;
                    }
                }

                try
                {
                    _context.Update(viewModel.Bag);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BagExists(viewModel.Bag.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }

            ViewData["CategoryID"] = new SelectList(_context.Categories, "ID", "Name", viewModel.Bag.CategoryID);
            ViewData["SupplierID"] = new SelectList(_context.Suppliers, "ID", "Name", viewModel.Bag.SupplierID);

            return(View(viewModel.Bag));
        }