public async Task <IActionResult> Add(AddItemViewModel model) { ShopItem newItem = new ShopItem(model.Name, model.Price, model.Description); if (!string.IsNullOrWhiteSpace(model.Categories)) { string[] categoryNames = model.Categories.Split(','); foreach (var categoryName in categoryNames) { string trimmedCategoryName = categoryName.Trim(); trimmedCategoryName = trimmedCategoryName.ToLower(); //if it isnt empty or null if (!string.IsNullOrWhiteSpace(trimmedCategoryName)) { ShopItemCategory existingCategory = _repository.GetCategoryByName(trimmedCategoryName); if (existingCategory == null) { ShopItemCategory newCategory = new ShopItemCategory(trimmedCategoryName); _repository.AddCategory(newCategory); newItem.Categories.Add(newCategory); } else { newItem.Categories.Add(existingCategory); } } } } _repository.AddItem(newItem); return(RedirectToAction("Index")); }