public async Task <IActionResult> Create([Bind("Id,Name,SummaryDescription,FullDescription,Price,ImageUrl,ImageThumbnailUrl,IsFavoriteSnack,InStock,CategoryId")] Snack snack) { if (snack.CategoryId == 0) { ModelState.AddModelError("CategoryId", "Selecione uma categoria"); } if (ModelState.IsValid) { var category = await _context.Categories .FindAsync(snack.CategoryId); if (category == null) { ModelState.AddModelError("CategoryId", "Selecione uma categoria válida"); } else { _context.Add(snack); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } } ViewData["CategoryItems"] = GetCategoryItems(); return(View(snack)); }
public async Task <IActionResult> Create([Bind("Id,Name,Description")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(category)); }