public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description")] Category category)
        {
            if (id != category.Id)
            {
                return(View("NotFound"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.Id))
                    {
                        return(View("NotFound"));
                    }
                    else
                    {
                        throw;
                    }
                }

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

            return(View(category));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,SummaryDescription,FullDescription,Price,ImageUrl,ImageThumbnailUrl,IsFavoriteSnack,InStock,CategoryId")] Snack snack)
        {
            if (id != snack.Id)
            {
                return(View("NotFound"));
            }

            if (snack.CategoryId == 0)
            {
                ModelState.AddModelError("CategoryId", "Selecione uma categoria");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var category = await _context.Categories
                                   .FindAsync(snack.CategoryId);

                    if (category == null)
                    {
                        ModelState.AddModelError("CategoryId", "Selecione uma categoria válida");
                    }
                    else
                    {
                        _context.Update(snack);
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SnackExists(snack.Id))
                    {
                        return(View("NotFound"));
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction("Index"));
            }

            ViewData["CategoryItems"] = GetCategoryItems();
            return(View(snack));
        }