Now we need to create an on post handler by default, only get handlers of created.
If you were working in an MVC environment, then you would create an on post action method.
But in razor pages, we recreate handlers, so creating handlers is simple.
We will call public and then the return type.
I want to make this an async method.
So I had async and task up action result, election result.
I'm using because I want to return to page or redirect to a few.
So that will be the return type, and we will call this on post.
When we have to name handlers, it will always start with the key word on.
And then we have the HTP word that this handler will do this handler will support the post action method.
That's why we will call on Post Handler.
Now, if you have multiple post handlers that you want in a reserve page, you can give this handler
name after the post like you can see on post create and then you can have on post edit.
That can be two post handlers.
But right now, we just want to create one post handler to fetch the details that the user entered and
create that category inside the database.
So here on post, we will receive a category object.
Let me call that as a category.
And let's create that inside our database in order to work the database.
We need dependency injection with the application context.
So first, let me create an application deep context where they do concordat on the using statement,
and I will require a constructor.
So you are and I with grief that from dependency injection and assign that to my local variable.
Then we want to create the category that we received right here so far that we will be using underscored
deep and we have to define our model, which is category on there.
We have more methods, we have methods to add, remove, update and much more.
The first thing that we want to do is add, you can use this add, but we have added the async method.
So let's use the async implementation.
Add async in that it expects a category entity that we have fetched with at on post.
So if it adds that when you are using async method, we will have to locate that before we proceed further.
When you write this one line, it is telling Entity Framework that, Hey, this is the category object
that you have to add inside the category table, but it does not tell entity framework to go and do
that right now in order to tell that that is a different statement.
That's a week and Discord DB don't be have save changes async when you write the save changes async
at that time, it gets all of the items that it has to change inside database and makes all of those
changes.
So if you forget to write save changes, it knows that it has to do that, but it will not save them
to the database and when it redirects or something that will be lost.
So always make sure to save the changes.
Once the changes are saved, we want to return back to the index page.
You see it displays redirect to action, but that is not valid in our application because we are using
razor pages with razor pages.
We have redirect to page and we want to redirect to the index page.
With that, let me save this at a debugging point.
And let me restart the application.
Since we've made changes to the code behind.
Let's go to our categories here.
We have create category.
Let me enter test and a display order hate the create button.
Now, as soon as you do that, if you examine the category, you can see they are populated.
Let's hit continue here.
And great, that is created.
So with this, we are able to create categories inside our application.
But I want to show you one more thing.
Let me close the application, and here we are fetching the category object Henriette, adding that
we already have a model here for category.
So it is something that is redundant.
To avoid that, that that protein has provided a special attribute, which is bind property, when you
use Bind property, you no longer have to write it here.
It automatically does the binding with the way that we have on the screen, and that will be populated
inside the same object.
So then we can use this category and add that like this.
This will make things much simpler.
Let's run the application and try to create one more category.
So let's create Test one this time.
And great, it is working as expected.
No, this is not needed right now, but I'd like to show you something else.
Let's say you had three properties that you were finding here and inside the post.
In that case, you will have to find property on each one of them from two point one.
They have added a special property that you can apply directly at the class level that does bind properties.
What that will do is all the properties that you have here and you are using inside that you.
It will find all of them automatically so that you do not have to individually explained property.
So with that, if you run, the application I create should still work the same fee, even though we
just have one category.
It's good to know that we have that option available if needed.
Let's try to create a new category attached to here.
And great, it's working.
So could this create functionality is working as expected?
Let's add some styling here and continue on the next radio.