//Get - Create public async Task <IActionResult> Create() { //to use SubCategoryandCategoryViewModel to create it's object with property - with value SubCategoryandCategoryViewModel model = new SubCategoryandCategoryViewModel() { 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)); }
public async Task <IActionResult> Delete(SubCategoryandCategoryViewModel model) { if (model.SubCategory == null) { return(NotFound()); } else { _db.SubCategory.Remove(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } }
public async Task <IActionResult> Create(SubCategoryandCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Where(s => s.Name == model.SubCategory.Name).Count(); var doesSubCategoryAndCatExists = _db.SubCategory.Where(s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).Count(); if (doesSubCategoryExists > 0 && model.isNew) { //error StatusMessage = "Error: SubCategory already Exists"; } else { if (doesSubCategoryExists == 0 && !model.isNew) { //error StatusMessage = "Error: SubCategory didn't Exists"; } else { if (doesSubCategoryAndCatExists > 0) { //error StatusMessage = "Error: SubCategory and Category combination already Exists"; } else { _db.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } } SubCategoryandCategoryViewModel modelVm = new SubCategoryandCategoryViewModel() { CategoryList = _db.Category.ToList(), SubCategory = new SubCategory(), SubCategoryList = _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToList(), StatusMessage = StatusMessage }; return(View(modelVm)); }
//Get Edit public async Task <IActionResult> Edit(int?Id) { if (Id == null) { return(NotFound()); } var subCategory = await _db.SubCategory.FindAsync(Id); if (subCategory == null) { return(NotFound()); } SubCategoryandCategoryViewModel model = new SubCategoryandCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
//Get Edit public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCategory = await _db.SubCategory.SingleOrDefaultAsync(m => m.Id == id); if (subCategory == null) { return(NotFound()); } SubCategoryandCategoryViewModel model = new SubCategoryandCategoryViewModel() { CategoryList = _db.Category.ToList(), SubCategory = subCategory, SubCategoryList = _db.SubCategory.Select(p => p.Name).Distinct().ToList() }; return(View(model)); }
public async Task <IActionResult> Edit(int id, SubCategoryandCategoryViewModel 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) { //status will be here StatusMessage = "Error : Sub Category Exists under " + doesSubCategoryExists.First().Category.Name + "Category, Please use another name. "; } else { var subCatfromDB = await _db.SubCategory.FindAsync(id); subCatfromDB.Name = model.SubCategory.Name; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } //to use SubCategoryandCategoryViewModel to create it's object with property - with value SubCategoryandCategoryViewModel modelVM = new SubCategoryandCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = new Models.SubCategory(), SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync(), StatusMessage = StatusMessage }; modelVM.SubCategory.Id = id; return(View(modelVM)); }
public async Task <IActionResult> Edit(int id, SubCategoryandCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Where(s => s.Name == model.SubCategory.Name).Count(); var doesSubCategoryAndCatExists = _db.SubCategory.Where(s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).Count(); if (doesSubCategoryExists == 0) { StatusMessage = "Error : SubCategory does not exists. You cannot add a new subcategory here"; } else { if (doesSubCategoryAndCatExists > 0) { StatusMessage = "Error : Category and SubCategory combination alreay exists. "; } else { var subCategoryfromDB = _db.SubCategory.Find(id); subCategoryfromDB.Name = model.SubCategory.Name; subCategoryfromDB.CategoryId = model.SubCategory.CategoryId; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } SubCategoryandCategoryViewModel modelVM = new SubCategoryandCategoryViewModel() { CategoryList = _db.Category.ToList(), SubCategory = model.SubCategory, SubCategoryList = _db.SubCategory.Select(p => p.Name).Distinct().ToList(), StatusMessage = StatusMessage }; return(View(modelVM)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCategory = await _db.SubCategory.SingleOrDefaultAsync(s => s.Id == id); if (subCategory == null) { return(NotFound()); } //to use SubCategoryandCategoryViewModel to create it's object with property - with value SubCategoryandCategoryViewModel model = new SubCategoryandCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }