public async Task <IActionResult> Create(SubCategoryAndCategoryViewModels model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategories.Include(e => e.Category).Where(e => e.Name == model.subCategory.Name && e.Category.Id == model.subCategory.CategoryId); if (doesSubCategoryExists.Count() > 0) { //Error StatusMessage = "Error: Sub Category exists under " + doesSubCategoryExists.First().Name + " category. Please use another name."; } else { _db.SubCategories.Add(model.subCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModels ModelVM = new SubCategoryAndCategoryViewModels() { CategoryList = await _db.Category.ToListAsync(), subCategory = model.subCategory, subCategoryList = await _db.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; return(View(ModelVM)); }
//GET Action for Create public IActionResult Create() { SubCategoryAndCategoryViewModels model = new SubCategoryAndCategoryViewModels() { CategoryList = _db.Category.ToList(), SubCategory = new SubCategory(), SubCategoryList = _db.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToList() }; return(View(model)); }
//--------------------------------------------------------------------------------------------// //Get -- CREATE public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModels model = new SubCategoryAndCategoryViewModels() { CategoryList = await _db.Category.ToListAsync(), subCategory = new Models.SubCategory(), subCategoryList = await _db.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync(), }; return(View(model)); }
public async Task <IActionResult> Create(SubCategoryAndCategoryViewModels model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategories.Where(s => s.Name == model.SubCategory.Name).Count(); var doesSubCatAndCatExists = _db.SubCategories.Where(s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).Count(); if (doesSubCategoryExists > 0 && model.isNew) { // error StatusMessage = "Error : Sub Category Name already exists"; } else { if (doesSubCategoryExists == 0 && !model.isNew) { //error StatusMessage = "Error : Sub Category does not exists"; } else { if (doesSubCatAndCatExists > 0) { //error StatusMessage = "Error : Category and Sub Category combination exists"; } else { _db.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } } SubCategoryAndCategoryViewModels modelVM = new SubCategoryAndCategoryViewModels() { CategoryList = _db.Category.ToList(), SubCategory = model.SubCategory, SubCategoryList = _db.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).ToList(), StatusMessage = StatusMessage }; return(View(modelVM)); }
public async Task <IActionResult> Edit(int id, SubCategoryAndCategoryViewModels model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategories.Where(s => s.Name == model.SubCategory.Name).Count(); var doesSubCatAndCatExists = _db.SubCategories.Where(s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).Count(); if (doesSubCategoryExists == 0) { StatusMessage = "Error : Sub Category does not exists. You cannot add a new Sub Category here."; } else { if (doesSubCatAndCatExists > 0) { StatusMessage = "Error : Category and Sub Category combination already exists."; } else { var subCatFromDb = _db.SubCategories.Find(id); subCatFromDb.Name = model.SubCategory.Name; subCatFromDb.CategoryId = model.SubCategory.CategoryId; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } SubCategoryAndCategoryViewModels modelVM = new SubCategoryAndCategoryViewModels() { CategoryList = _db.Category.ToList(), SubCategory = model.SubCategory, SubCategoryList = _db.SubCategories.Select(p => p.Name).Distinct().ToList(), StatusMessage = StatusMessage }; return(View(modelVM)); }
//GET Edit public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCategory = await _db.SubCategories.SingleOrDefaultAsync(m => m.Id == id); if (subCategory == null) { return(NotFound()); } SubCategoryAndCategoryViewModels model = new SubCategoryAndCategoryViewModels() { CategoryList = _db.Category.ToList(), SubCategory = subCategory, SubCategoryList = _db.SubCategories.Select(p => p.Name).Distinct().ToList() }; return(View(model)); }