public async Task <IActionResult> Create(SubCategorieEnCategorieViewModel model) { if (ModelState.IsValid) { var doesSubCategorieExists = _context.SubCategorie.Include(s => s.Categorie).Where(s => s.Subcategorie == model.SubCategorie.Subcategorie && s.Categorie.Id == model.SubCategorie.CategorieId); if (doesSubCategorieExists.Count() > 0) { //Error StatusBericht = "Foutmelding : Sub-Categorie bestaat reeds onder categorie " + doesSubCategorieExists.First().Categorie.Naam + ". Kies een andere naam aub."; } else { _context.SubCategorie.Add(model.SubCategorie); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategorieEnCategorieViewModel modelVM = new SubCategorieEnCategorieViewModel() { CategorieLijst = await _context.Categorie.ToListAsync(), SubCategorie = model.SubCategorie, SubCategorieLijst = await _context.SubCategorie.OrderBy(p => p.Subcategorie).Select(p => p.Subcategorie).ToListAsync(), StatusBericht = StatusBericht }; return(View(modelVM)); }
//GET- CREATE public async Task <IActionResult> Create() { SubCategorieEnCategorieViewModel model = new SubCategorieEnCategorieViewModel() { CategorieLijst = await _context.Categorie.ToListAsync(), SubCategorie = new Models.SubCategorie(), SubCategorieLijst = await _context.SubCategorie.OrderBy(p => p.Subcategorie).Select(p => p.Subcategorie).Distinct().ToListAsync() }; return(View(model)); }