public IActionResult GetCategoryById(int categoryId) { var category = _categoryRepository.GetCategoryById(categoryId); if (category == null) { ModelState.AddModelError("", "Error getting a category"); ViewBag.Message = $"There was a problem retrieving category with id {categoryId} " + $"From the database or no category with that id exists"; category = new CategoryDto(); } var books = _categoryRepository.GetAllBooksForCategory(categoryId); if (books.Count() <= 0) { ViewBag.BookMessage = $"{category.Name} has no books"; } var bookCategoryViewModel = new CategoryBooksViewModel() { Category = category, Books = books }; // because of create / update / delete method so we have to have it ViewBag.SuccessMessage = TempData["SuccessMessage"]; return(View(bookCategoryViewModel)); }
public IActionResult GetCategoryById(int categoryId) { var category = _categoryRepository.GetCategoryById(categoryId); if (category == null) { ModelState.AddModelError("", $"There is no category with id:{categoryId}"); ViewBag.Message = $"There was a problem retrieving category with id {categoryId} " + $"from the database or no category exists"; category = new CategoryDto(); } var books = _categoryRepository.GetAllBooksForCategory(categoryId); if (books.Count() <= 0) { ViewBag.BookMessage = $"Category: {category.Name} has no books"; } var bookCategoryViewModel = new CategoryBooksViewModel() { Category = category, Books = books }; return(View(bookCategoryViewModel)); }