I am new to asp.net so easy on me :D, I am builiding a simple CRUD app, I managed to get the lists and routing correctly, but now I want to get a category's book.
So that's my Book & Category Entities :
public class Book
{
[Key]
public int bookId { get; set; }
public string bookName { get; set; }
public double bookPrice { get; set; }
public string bookImageUrl { get; set; }
[ForeignKey("Category")]
public int categoryId { get; set; }
public Category category { get; set; }
}
public class Category
{
[Key]
public int categoryId { get; set; }
public string categoryName { get; set; }
public string categoryDescription { get; set; }
public List<Book> books { get; set; }
}
This is my category repository, the second method is the one, I am including the books :
public class CategoryRepository : ICategoryRepository
{
private readonly AppDbContext _appDbContext;
public CategoryRepository(AppDbContext appDbContext)
{
_appDbContext = appDbContext;
}
public IEnumerable<Category> getAllCategories()
{
return _appDbContext.Categories;
}
public Category getCategoryById(int categoryId)
{
return _appDbContext.Categories.Include(category => category.books).FirstOrDefault(category => category.categoryId == categoryId);
}
}
This is my CategoryController even though I don't the probleme is here :
private readonly ICategoryRepository _categoryRepository;
public CategoryController(ICategoryRepository categoryRepository)
{
_categoryRepository = categoryRepository;
}
public IActionResult categoryBooks(int idCategory)
{
Trace.WriteLine("this " + idCategory);
var booksListViewModel = new BooksListViewModel();
booksListViewModel.Books = _categoryRepository.getCategoryById(idCategory).books;
return View(booksListViewModel);
}
}
I have one book & one category in the database and that's it :
Any ideas ? Any guiding ?
Any help would be much appreciated.
