public async Task <IActionResult> Create(SubCategoryAndCategoryViewFolder model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId); if (doesSubCategoryExists.Count() > 0) { //Error StatusMessage = "Error : Sub Category exists under " + doesSubCategoryExists.First().Category.Name + " category. Please use another name!"; } else { _db.SubCategory.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewFolder modelVM = new SubCategoryAndCategoryViewFolder() { CategoryList = await _db.Category.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; return(View(modelVM)); }
public async Task <IActionResult> Delete(int id, SubCategoryAndCategoryViewFolder model) { if (ModelState.IsValid) { { var subCatFromDb = await _db.SubCategory.FindAsync(id); _db.SubCategory.Remove(subCatFromDb); //_db.SubCategory.Update(subCatFromDb); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewFolder modelVM = new SubCategoryAndCategoryViewFolder() { CategoryList = await _db.Category.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; modelVM.SubCategory.Id = id; return(View(modelVM)); }
//GET - CREATE public async Task <IActionResult> Create() { SubCategoryAndCategoryViewFolder model = new SubCategoryAndCategoryViewFolder() { CategoryList = await _db.Category.ToListAsync(), SubCategory = new Models.SubCategory(), SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
//GET - DETAIL public async Task <IActionResult> Details(int id, SubCategoryAndCategoryViewFolder model) { if (ModelState.IsValid) { // var subCatFromDb = await _db.SubCategory.FindAsync(id); } var subCatFromDb = await _db.SubCategory.FindAsync(id); SubCategoryAndCategoryViewFolder modelVM = new SubCategoryAndCategoryViewFolder() { CategoryList = await _db.Category.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), }; modelVM.SubCategory = subCatFromDb; return(View(modelVM)); }
//GET - DELETE public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } var subCategory = await _db.SubCategory.SingleOrDefaultAsync(m => m.Id == id); if (subCategory == null) { return(NotFound()); } SubCategoryAndCategoryViewFolder model = new SubCategoryAndCategoryViewFolder() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }