All about the Category

Rose Chang Dycd
4 min readMar 9, 2022

of ASP.NET Core (.Net 6) — 06

Add a new controller by pressing right-mouse, add, controller.

Add a new View for the added controller. Click the right mouse, add view, choose the second one, Add.

Then, there are a few configurations that you have to do when adding a raised view:

  1. What was the name of the View? We will give that the same name as the action method, which is the index.

2. Do you want to use any template when you create this view? If you hover at the second column, you can create a view for creatively detailed edits and lists. I do not want to go into those details right now, but we will come back to that later on. If you change that, then the model and data context class will be enabled.

3. Partial View. Partial view is basically like user controls in the platform, so it could be rendered inside some other view. If you select a partial view, the layout page will not be used because you do not need any master page.

4. If you want to use the default master page and keep it blank, it will by default use the layout that we have set inside to view stock, which is on _Layout.cshtml.

Now we are going to make the application retrieve the data from DB.

We firstly set up some dummy data in the Categories table.

In the application, we know that we have set up the table name in Data → ApplicationDbContext.cs.

So how can we create an object of this application, be context and use that to court our database and table?

That is the beauty of dependency injection. We do not have to create an object of this class. Everything will be done for us because we have configurations inside our container.

We want to use this service. So because of dependency injection, we don’t have to create its object, we can paint that object is already dead. We just have to tell the application that please send me the object of the application DB context.

How do we request that?

Inside the controller, we want the application to be context to work with the database. So we add as below:

We need to tell our application that we need an implementation of this application to be a context where the connection to the database is already made. And I can retrieve some records right away so that you will have to use the constructor. Now enter ctor and press the Tab key. Then enter the ApplicationDbContext in the quote and make the DB equal to _db.

Then we create a syntax to retrieve the DB list. What first access to _db and we want to work on right now is categories. And we want to convert it to a list and retrieve that.

So it will go to the database, it will retrieve all of the categories. It will convert that to a list and it will assign that inside the categories list. You can see how beautiful this is. You do not have to write a select statement to retrieve all the categories from the table. There is no SQL coding required. Add a debugging dot and check whether it works!

You can see it exactly goes to the database, it fetches the categories, and it does that automatically.

Now that we have all the categories, in fact, this particular object. We can copy and paste the objCategoryList in the View().

Let’s change the variables to IEnumerable<Category> and using BulkyBookWeb.Models;.

Now we pass the Categories data to the View, we have to make the View can capture the data as well.

--

--