public ActionResult Create(SubCategoryFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Categories = this.GetCategories();
                return(View(model));
            }

            string name = model.SubCategory.Name;

            if (this.subCategoryService.NameExists(name))
            {
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, name));
                model.Categories = this.GetCategories();
                return(View(model));
            }

            bool success = this.subCategoryService.Create(model.CategoryId, name);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage(string.Format(
                                           WebConstants.SuccessfullEntityOperation,
                                           SubCategory,
                                           WebConstants.Added));

            return(RedirectToAction(
                       nameof(CategoriesController.All),
                       Categories,
                       new { area = WebConstants.ModeratorArea }));
        }
        public async Task <IActionResult> Create(SubCategoryFormViewModel formViewModel)
        {
            if (ModelState.IsValid)
            {
                var subCategoriesThatExist = _context.SubCategories.Include(s => s.Category).Where(s =>
                                                                                                   s.Name.ToLower() == formViewModel.SubCategory.Name.ToLower() && s.CategoryId == formViewModel.SubCategory.CategoryId);

                if (subCategoriesThatExist.Any())
                {
                    StatusMessage = $"Error : Sub Category with the same name exists in Category - {subCategoriesThatExist.First().Category.Name}.";
                }
                else
                {
                    _context.SubCategories.Add(formViewModel.SubCategory);
                    await _context.SaveChangesAsync();

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

            var pageVm = new SubCategoryFormViewModel
            {
                CategoryList    = await _context.Categories.ToListAsync(),
                StatusMessage   = StatusMessage,
                SubCategory     = formViewModel.SubCategory,
                SubCategoryList =
                    await _context.SubCategories.OrderBy(sc => sc.Name).Select(sc => sc.Name).ToListAsync()
            };

            return(View(pageVm));
        }
        public ActionResult Create()
        {
            SubCategoryFormViewModel model = new SubCategoryFormViewModel
            {
                Categories = this.GetCategories()
            };

            return(View(model));
        }
        public async Task <IActionResult> Create()
        {
            var viewModel = new SubCategoryFormViewModel
            {
                CategoryList    = await _context.Categories.ToListAsync(),
                SubCategory     = new SubCategory(),
                SubCategoryList =
                    await _context.SubCategories.OrderBy(sc => sc.Name).Select(sc => sc.Name).ToListAsync()
            };

            return(View(viewModel));
        }
        public ActionResult Edit(int?id, SubCategoryFormViewModel model)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                model.Categories = this.GetCategories();
                return(View(model));
            }

            string oldName = this.subCategoryService.GetName(id.Value);

            if (oldName == null)
            {
                return(BadRequest());
            }

            string newName = model.SubCategory.Name;

            if (this.subCategoryService.NameExists(newName) &&
                oldName != newName)
            {
                model.Categories = this.GetCategories();
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, newName));
                return(View(model));
            }

            bool success = this.subCategoryService.Edit(id.Value, model.CategoryId, newName);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage(
                string.Format(WebConstants.SuccessfullEntityOperation,
                              SubCategory,
                              WebConstants.Edited));

            return(RedirectToAction(
                       nameof(CategoriesController.All),
                       Categories,
                       new { area = WebConstants.ModeratorArea }));
        }
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var subCategoryFromDb = await _context.SubCategories.FindAsync(id);

            var viewModel = new SubCategoryFormViewModel
            {
                CategoryList    = await _context.Categories.ToListAsync(),
                SubCategory     = subCategoryFromDb,
                SubCategoryList =
                    await _context.SubCategories.OrderBy(sc => sc.Name).Select(sc => sc.Name).ToListAsync()
            };

            return(View(viewModel));
        }
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            SubCategoryFormViewModel model = new SubCategoryFormViewModel
            {
                SubCategory = this.subCategoryService.GetForm(id.Value),
                Categories  = this.GetCategories()
            };

            if (model.SubCategory == null)
            {
                return(BadRequest());
            }

            return(View(model));
        }