public async Task <IActionResult> Create(SubcategoryandcategoryViewmodel model)
        {
            if (ModelState.IsValid)
            {
                var subCategories = await _db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId).ToListAsync();

                if (subCategories.Count() > 0)
                {
                    StatusMessage = "Error: SubCategory exists under" + subCategories.First().Category.Name + "category. Please use another name.";
                }
                else
                {
                    _db.SubCategory.Add(model.SubCategory);
                    await _db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            SubcategoryandcategoryViewmodel subcategoryandcategoryViewmodel = new SubcategoryandcategoryViewmodel
            {
                CategoryList    = await _db.Category.ToListAsync(),
                SubCategory     = model.SubCategory,
                SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(),
                StatusMesssage  = StatusMessage
            };

            return(View(subcategoryandcategoryViewmodel));
        }
        public async Task <IActionResult> Delete(int id, SubcategoryandcategoryViewmodel model)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return(NotFound());
                }
                else
                {
                    var subCategory = await _db.SubCategory.FindAsync(id);

                    if (subCategory == null)
                    {
                        return(NotFound());
                    }
                    _db.SubCategory.Remove(subCategory);
                    await _db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            SubcategoryandcategoryViewmodel subcategoryandcategoryViewmodel = new SubcategoryandcategoryViewmodel
            {
                CategoryList    = await _db.Category.ToListAsync(),
                SubCategory     = model.SubCategory,
                SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(),
                StatusMesssage  = StatusMessage
            };

            subcategoryandcategoryViewmodel.SubCategory.Id = id;
            return(View(subcategoryandcategoryViewmodel));
        }
        public async Task <IActionResult> Create()
        {
            SubcategoryandcategoryViewmodel subcategoryandcategoryViewmodel = new SubcategoryandcategoryViewmodel
            {
                CategoryList    = await _db.Category.ToListAsync(),
                SubCategory     = new SubCategory(),
                SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync()
            };

            return(View(subcategoryandcategoryViewmodel));
        }
        //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 subcategoryandcategoryViewmodel = 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(subcategoryandcategoryViewmodel));
        }