Im using visual studio with ASP.NET Core web App (.NET 6.0).Im using Microsoft SQL Server Management Studio 19 as database. Currently, i ran a problem where it display the id instead of name in book index.
I have category table in the database and show it in drop box
But when I show product detail in the index, the category become ID of the category
here is my code: https://github.com/thorgia1702/FPTBook
I have tried make the category name become the FK key in the book table in the Migrations file but have not success
2
Answers
I think the issue with this method is that you should include the foreign object. The reason is that
ToListAsync()
materialises the query, and if you don’t call Include, the category data will not load.I also found that when you add a new book, you should use the
CategoryId
instead ofCategory
.I test your project, and I have the idea like below, could you try the below code?
<Nullable>enable</Nullable>
In BookController:
1.change the index action to add .Include to use the Include method to specify related data to be included in query results. :
2.Change the post create action like your create action in CategoryController:
3.result: