public async Task <IActionResult> EditPost(CategoryPlusErrorMsgViewModel categoryVM) { if (ModelState.IsValid) { var cat = await _db.Categories.FindAsync(categoryVM.Category.Id); if (cat == null) { return(NotFound()); } var doesCategoryExists = await _db.Categories.FirstOrDefaultAsync(c => c.Name == categoryVM.Category.Name); if (doesCategoryExists != null) { categoryVM.StatusMessage = "Error : Category " + doesCategoryExists.Name + " exists! Please use another name."; return(View(categoryVM)); //category exists... } else { cat.Name = categoryVM.Category.Name; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } return(View(categoryVM)); }
//Edit Get public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var category = await _db.Categories.FindAsync(id); if (category == null) { return(NotFound()); } CategoryPlusErrorMsgViewModel model = new CategoryPlusErrorMsgViewModel(); model.Category = category; model.StatusMessage = null; return(View(model)); }