public async Task <ActionResult> Create(SubCategoryCreateModel model) { if (!ModelState.IsValid) { await FillCategoryAndCategoryList(model); return(View(model)); } var category = await _categoryRepository.GetCategoryByNameAsync(model.CategoryName); if (category.SubCategories.FirstOrDefault(x => x.Name == model.Name) != null) { ModelState.AddModelError(nameof(SubCategoryCreateModel.Name), "SubCategory with this name already exists in that category"); await FillCategoryAndCategoryList(model); return(View(model)); } try { var subCategory = await _subCategoryService.AddSubcategory(model.Name, model.CategoryName); return(RedirectToAction(nameof(ListFromCategory), new { id = subCategory.CategoryId })); } catch (Exception ex) { ViewData["Error"] = ex.Message; await FillCategoryAndCategoryList(model); return(View(model)); } }
// GET: SubCategory/Create public async Task <ActionResult> Create() { var model = new SubCategoryCreateModel(); await AddCategories(model); return(View(model)); }
private async Task FillCategoryAndCategoryList(SubCategoryCreateModel model) { if (model.CategoryName == null) { model.CategoryName = string.Empty; } if (model.CategoriesNames == null) { await AddCategories(model); } }
private async Task AddCategories(SubCategoryCreateModel model) { var categories = await _categoryService.GetAllCategoriesNames(); model.CategoriesNames = categories; }