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)); } }