public async Task <IActionResult> DeleteConfirmed(int?id)
        {
            var category = await _db.Category.FindAsync(id);

            if (category == null)
            {
                return(View());
            }

            CategoryDeleteVM objCD = new CategoryDeleteVM {
                Category = category
            };

            var IsLinkedSubCategory = await _db.SubCategory.Where(s => s.CategoryId.Equals(id)).ToListAsync();

            var IsLinkedMenuItem = await _db.MenuItems.Where(s => s.CategoryId.Equals(id)).ToListAsync();

            if (IsLinkedSubCategory.Count > 0)
            {
                objCD.StatusMessage = "Error : Unable to delete \"" + category.Name + "\", because it is linked to a Sub-Category.";
                return(View(objCD));
            }
            else if (IsLinkedMenuItem.Count > 0)
            {
                objCD.StatusMessage = "Error : Unable to delete \"" + category.Name + "\", because it is linked to a Menu Item.";
                return(View(objCD));
            }


            _db.Category.Remove(category);
            await _db.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Example #2
0
        public void DeleteMappedCategory(CategoryDeleteVM vm)
        {
            // aşşağıdaki kullanım şeklinde, instance almadan dbset içerisindeki kaydı map işlemi yapar.
            Category category = _mapper.Map(vm, _categoryRepository.GetById(vm.Id));

            _categoryRepository.Delete(category);
        }
        //Get-Delete
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var category = await _db.Category.FindAsync(id);

            if (category == null)
            {
                return(NotFound());
            }
            CategoryDeleteVM objCD = new CategoryDeleteVM {
                Category = category
            };

            return(View(objCD));
        }