示例#1
0
        public async Task <IActionResult> Delete(int ID, SubCategoryandCategoryModel model)
        {
            if (ModelState.IsValid)
            {
                var subCategoryExists = dbContext.SubCategories
                                        .Include(s => s.Category)
                                        .Where(s => s.Name == model.SubCategory.Name &&
                                               s.Category.CategoryID == model.SubCategory.ID);

                if (subCategoryExists.Count() > 0)
                {
                    StatusMessage = "Error : Sub Category exist under" + subCategoryExists.First().Category.Name;
                }

                else
                {
                    var subCatFromDB = await dbContext.SubCategories.FindAsync(ID);

                    dbContext.SubCategories.Remove(subCatFromDB);

                    await dbContext.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }

            var _model = new SubCategoryandCategoryModel()
            {
                Categories          = dbContext.Categoris.ToList(),
                SubCategory         = model.SubCategory,
                SubCategoryListName = dbContext.SubCategories.OrderBy(sub => sub.Name).Select(s => s.Name).ToList(),
                StatusMessage       = StatusMessage
            };

            return(View(_model));
        }