YTread Logo
YTread Logo

Learn ASP.NET Core 3.1 - Full Course for Beginners [Tutorial]

Jun 05, 2021
if   used to create or update. This looks good for the absurd feline method of action. In the next video, let's add the absurd view. In this video, let me add the absurd view, we can right click on the action method. And we'll click Add View. This looks good. Let's add this. Now in the opposite view, first what will be the model, if we take a look at the book controller, we are passing a book object to the view. So that will be the model. Then we will define the model. It's the MVC dot models dot book list, we're going to add a br and we're going to add a header here to give it a text information class.
learn asp net core 3 1   full course for beginners tutorial
Here we want to show edit book or create book as per the request. If we go back to our controller, if the ID is now, we pass the book object with a null ID. So if Id is null then it is a create request. So here and now it will be zero. So we can check and we will say if model.id is not equal to zero. If it is not zero, edit will be displayed; otherwise only create will be displayed. Along with that we want to show the book. In this way, the single line will serve both to create an edition book and to display the title.
learn asp net core 3 1   full course for beginners tutorial

More Interesting Facts About,

learn asp net core 3 1 full course for beginners tutorial...

We'll add br here and we'll add mean to give it a border class and we'll add container, we'll give it a style for a 30 pixel padding. Here, let me add a form tag and we will give you the post method. So whenever a user submits the button, it will go to the post action method for absurd inside the book controller. Here you can also define the action method as absurd. But since we're in the absurd, if you didn't define this, that will work too. If it were some other action method compared to the name, then you would have to define it, but in this case we are fine.
learn asp net core 3 1   full course for beginners tutorial
Now when we publish it, we will need the id if the id is not there now, so we will add a fear. And we will check if model.id is non-zero, then I want to display that ID in a hidden property. So the input type is equal to hidden ASP, four will be the ID. Then, just like in Razor Pages, we want validations here. So we'll add a text hazard class. And we will use ASP Validation Summary   only for the model. Then we will start displaying our property in the group and row class of the form.
learn asp net core 3 1   full course for beginners tutorial
And in each of them, we'll add a div and give it a column class three. First, we want to display the label   with ASP so that it is named. And we'll add a div and give it a column class six here, we want the input type. Then enter ASP for the auxiliary tag. This will be for the name of the book and we will give it a form control class. On top of that, we'll add an ASP-validated range to our name and give it a text hazard class. This looks good for our first property. Let me copy   this and paste it two more times.
Let me do it three times, after the name, the author will copy this and paste it here. And we have ESPN will copy and paste it here. Once we complete the ESPN, next will be the create or update button and a link to return to the page. So we'll have column three here, let me also make it with an offset of three. And this will be three. Here,   let me have a Send type pattern. Since we're in a shape, this will be good. We will add btn btn,   primary and form control. So what we want to display on the button is the same logic that we had here.
So we can copy this and we'll paste it here. Instead of edit, we will show update or create. The following is a link to return. Here we can display an anchor tag with the Tag Helper ESP action. If we click on this action, we want to go to the index section of the book controller. Since we're already in the book controller, it's okay if you don't write the ASP controller here. And enough with the ASP action. We'll add some bootstrap classes here between the success and the form control and show them back in the list. That being said, let's save this.
And let's run our application. Here, if you go to the book list and we don't have any data, let's add a new book and press Create. Great, we have the validations. But when we hit, it comes back. We don't want that. For that we will add the scripts right here. So we'll add section scripts. And here we will add the partial name. If we go to our shared, we have partial validation scripts that need to be renamed and copied. And we'll paste it right here. Let's run our application and try again. Let's go to the book list.
Let's try to add a plugin build, this time it didn't do a postback and showed validation, we'll fill in some details and press Create. Nothing will happen. The reason is that we haven't set the post action method to be nonsense. Let's do it   in the next video. Now that we've added the silly view, what will happen when we hit a button with all the details? For that we need to add an absurd post action method. So we can just copy   this and paste it one more time. This time we must mention that it is an HTTP POST.
This time we must mention that it is an HTTP POST. And with the post methods, we have to use a validation anti-forgery token to use the built-in security and prevent some attacks. Said this in absurdity. Right here you can use book. And we'll retrieve the object like this when you submit the form, but if you go to the top right here you're using the bind property, so you don't need it, you can directly access the book object that we have inside our controller. Now, within the absurdity, what is the first thing we should check? We must check whether the state of the model is valid or not.
So if the model state is valid, only then continue. If it is valid, then we will check here, if both.id is equal to zero, if it is zero, it means it is a creation. And we will underline DB dot books dot add. And we'll add the book object there. Otherwise it's for   an update. So we will underline db.books. Here we have an update method. And we just need to pass the object that needs to be updated, it will update it based on the ID inside this book object. Once the whole system, we need to underline DB point Save Changes to send all the changes to our database.
Once the changes have been implemented we want to go back to the razor pages we were using redirect to page in MVC, we will use redirect to action and we can redirect to index action. So what it will do is after saving the changes, it will redirect to the index action here and load all the workbooks again from the database. This looks good. Let's run this   and test it. Let's go to the book list here, add a new book, fill in some details and hit the Create button. Excellent. You can see that our data tables also work with the new workbook.
Let's try to update this. You can see how to create an edit and both features work as expected. The only thing left is to delete here. Now when you click delete,   you will see a nice alert. And if you click OK here, the workbook should be deleted. And it shows   a nice toaster notification. You can see that our create, edit and delete functions work as expected. Now with this you can see how we can perform CRUD operations on the list of books using an MVC project. And this is just the beginning. There is much more to explore in ASP.
NET

core

. Whether you go with MVC or Razor Pages, what we just

learn

ed is the tip of the iceberg. So if you are interested in

learn

ing more about MVC or Razor Pages, I have many more detailed

course

s where we will create real-world applications. But that's all we had for this

course

and good luck with your future projects.

If you have any copyright issue, please Contact