public async Task <IActionResult> Create(SubCategoryAndCategoryModel 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)));//Nếu thành công thì Di chuyển tới Action Index } } SubCategoryAndCategoryModel modelVM = new SubCategoryAndCategoryModel() { 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));//Nếu đã có SubCategory tồn tại trong Category rồi thì trả về View Create có chứa đối tương modelVM }
public async Task <bool> EditPostSubCategory(SubCategoryAndCategoryModel model) { var subCategory = await GetItemAsync(model.SubCategoryModel.Id); subCategory.NameSubCategory = model.SubCategoryModel.Name; subCategory.CategoryId = Int32.Parse(model.SubCategoryModel.CategoryId); return(await UpdateItem(subCategory)); }
public async Task <bool> CreatePostSubCategory(SubCategoryAndCategoryModel model) { SubCategory subCategory = new SubCategory { NameSubCategory = model.SubCategoryModel.Name, CategoryId = Int32.Parse(model.SubCategoryModel.CategoryId) }; return(await AddItemAsync(subCategory)); }
public async Task <SubCategoryAndCategoryModel> CreateSubCategoryModel(ICategoryRepository iCategoryRepository) { SubCategoryAndCategoryModel model = new SubCategoryAndCategoryModel { Categories = await iCategoryRepository.GetAllCategory(), SubCategoryModel = new SubCategoryModel() }; return(model); }
public async Task <IActionResult> Create() { SubCategoryAndCategoryModel model = new SubCategoryAndCategoryModel() { 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> Edit(int?id) { SubCategoryAndCategoryModel model = await iSubCategoryRepositiry.EditGetSubCategory(id, iCategoryRepository); if (model == null) { return(NotFound()); } return(View(model)); }
public async Task <SubCategoryAndCategoryModel> ExistSubCategory(SubCategoryAndCategoryModel model, ICategoryRepository iCategoryRepository) { var exist = await AllItems.Include(z => z.Category).Where(x => x.NameSubCategory == model.SubCategoryModel.Name && x.Category.Id == Int32.Parse(model.SubCategoryModel.CategoryId)).ToListAsync(); if (exist.Count > 0) { model.StatusMessage = "Error: Sub category exist under" + model.SubCategoryModel.Name + "category. Please use another name."; model.SubCategoryModel.Category = await iCategoryRepository.AllItems.FirstOrDefaultAsync(z => z.Id == Int32.Parse(model.SubCategoryModel.CategoryId)); model.Categories = await iCategoryRepository.GetAllCategory(); return(model); } return(null); }
public async Task <IActionResult> Create(SubCategoryAndCategoryModel model) { if (ModelState.IsValid) { SubCategoryAndCategoryModel sub = await iSubCategoryRepositiry.ExistSubCategory(model, iCategoryRepository); if (sub != null) { return(View(sub)); } if (!await iSubCategoryRepositiry.CreatePostSubCategory(model)) { return(NotFound()); } return(RedirectToAction(nameof(Index))); } return(View(model)); }
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()); } SubCategoryAndCategoryModel model = new SubCategoryAndCategoryModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
public async Task <SubCategoryAndCategoryModel> EditGetSubCategory(int?id, ICategoryRepository iCategoryRepository) { if (id == null) { return(null); } var subCategory = await AllItems.Include(z => z.Category).FirstOrDefaultAsync(x => x.Id == id); if (subCategory == null) { return(null); } SubCategoryModel sub = new SubCategoryModel { Id = subCategory.Id, Name = subCategory.NameSubCategory, Category = subCategory.Category }; SubCategoryAndCategoryModel model = new SubCategoryAndCategoryModel { Categories = await iCategoryRepository.GetAllCategory(), SubCategoryModel = sub }; return(model); }
public async Task <IActionResult> Edit(SubCategoryAndCategoryModel 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); //Câu lệnh ở trên Chọn ra các SubCategory có cùng tên và cùng Id Category từ Database với Đối tượng có Model truyền vào. if (doesSubCategoryExists.Count() > 0) { //Error StatusMessage = "Error : Sub Category exists under " + doesSubCategoryExists.First().Category.Name + " category. Please use another name."; } else { var subCatFromDb = await _db.SubCategory.FindAsync(model.SubCategory.Id); //Lấy ở Table SubCategory dưới Database đối tượng có cùng id rồi gán nó vào subCatFormDb subCatFromDb.Name = model.SubCategory.Name; //Cập nhật subCatFromBb. Cụ thể là cập nhật thuộc tính Name. Nên nhớ ở đây là subCatFromDb đang được tracking với ModelState là Update. await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryModel modelVM = new SubCategoryAndCategoryModel() { 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)); }