ASP.NET Core Razor Pages Full CRUD - .NET 7.0 Razor Pages using Entity Framework Core and SQL Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome back in today's session we are going to perform crowd operations in asp.net core by using razor pages so first we'll see the demo I am going to click on product so this index page will be displayed here currently we don't have any products that's why we are displaying this message to the user whenever we have the products so we are going to bind into this table we will create one product here we'll click on create new this is the create page where we can create new product I am trying to create product without entering any values I'll click on create so client-side validation and server side validations implemented in this example now I'll enter the values here I'll click on create once product is created it will be redirected to index page from index page we can edit and we can see the details and we can delete the product first we will see the edit page click on edit so it will redirect it to edit page here we are posting the product ID which is primary key in our table I am going to update these details here save it so product details has been updated once it is updated again we are redirecting into index page I'll click on the details page in this Details page product details will be available we can go to back to list so again it will be redirected to index page if you want to edit the product here you can click on edit I am going to back to list so now here we can delete the product click on delete once we click on delete so it will be redirected to delete page where we are passing product ID again here user has to confirm the deletion so once user is clicked on this delete button this product will be deleted permanently from the database if I don't want to delete I can click on back to list now again I'll go to the delete this time I am going to delete product click on delete product has been deleted and default message is displayed since we don't have any products inside the table so this default message will be displayed here for this example we are going to use the visual studio and SQL server and we are going to use the Entity Framework code first approach also here we are going to use the scaffolding mechanism we are not going to write any code in upcoming videos we are going to perform the crowd operations without using this cap folding mechanism also we are going to perform crud operations by using the stored procedures for now we are going to use the scaffolding mechanism in this session here I am going to use Visual Studio 2022 I am going to use the existing solution to create new project here we have to create the new application click on the solution click on add new project here I am searching for dotnet core application here we have to select the asp.net core web application earlier we used to select the asp.net core web application with MVC here we are not going to use the MVC we are going to use the razor Pages for this I am going to select asp.net core web app click on next here we have to provide the project name this is my project name I am going to store in this location click on next I am going to select the latest.net framework here in my case I am selecting dotnet 7.0 and click on the create new project will be created for us compared to MVC application there is a slight differences with the Eraser application so here we don't have any modal view controller earlier we had created MVC application which we had models views and controllers separate folders here nothing will be there and the root folder will be similar to the MVC where all our static files folders will be available here and apart from this we have app settings.json it is similar to MVC and the program.cs and here new thing is we have folder called pages so where all our razor pages will be created inside the pages folder if you see this shared folder here our layout will be available inside the shared folder we will make this project as default project say test Auto project we will run the application and we will see the output this is the home page if I click on privacy it will be redirected to privacy page again if I click on home page it will be redirected to home page I'll close this here I am going to create one folder called models here I am going to Define my database entities I will right click on this project add a new folder here I'm going to create one folder called models inside this I am going to Define my DB entities so here I am going to create one class I'm creating one class called Product here I am going to Define my properties like it as ID this is going to be primary key I will decorate with key then I need another property called Product name which is going to be string so this is the required field so we have to add the attribute called required also I am going to change the display name for binding purpose so these are all the properties I am going to define it here we have to add the required namespace this should be display name price also mandatory field also quantity is mandatory so this is our product model go to the application I'll create one more folder here which is called dial I'll create a class here this is my contacts name here we are going to use the Entity framework code first approach for that we have to add the required namespaces right click on the project go to the manage you get packages here we can search for Entity framework core I need Entity framework code install it accept the license terms then we need SQL Server which we are going to use it for our DB connectivity accept also we are going to use this scaffolding for that we have to install the design install it here we are going to use the code first approach for that we have to install the tools package install the latest version accept the terms so we have installed all the packages related to Entity Framework close this here I have DB context will be inherited from the DB context here we have to add the required namespace which is Entity framework core you have to select the Entity framework code select the DB context go to the quick actions generate Constructor with options here we have to add one more property which is going to be virtual and it's a DB set here we have to call our product so we are going to use this DB context to communicate between database and our application I'll make it as products to communicate with database we need to provide the DB name and connection strings for that we have to go to the app settings.json here we are going to add the connection string here we have to provide the connection string name I'm going to paste it here this is the connection string name and here I am providing my SQL server name in my SQL Server I have multiple SQL instances that's why I am providing SQL and stress name this is my PC name and this is my SQL instance name if you have single instance you now need to provide this instance name this is my database name here I am going to use the windows authentication that's why I mentioned as Integrated Security true here I am going to use the SSL that's why I'm going to make it as trust server certificate as true now we have to add the dependency injection for this connection string this is the dependency injection it's a fixed code we don't need to change anything go to the program.cs before Builder dot build here we have to call our dependency injection so here we are going to use this SQL server and here we have to pause the connection string name also we have to pass the app DB context name here so I'll copy my appdb context name from here I'm going to pause it here here we have to add the namespace so this appdb context is available inside the dial folder so we have to add the namespace save it we will build the application once so there is no error first I am going to migrate our database into the SQL server for that I am going to use the package manager console go to the package manager console here we have to select the default project in our case crud with razor Pages this is our project here I am going to add migration command first I have to provide the migration name I will provide as initial enter so build succeeded now we have to update the database now database has been migrated we will switch to SQL server and we'll check that this is my SQL Server I'll refresh the databases so this is our database name so database is created and we can check the tables also so table also created we can check the columns and data types so all four columns are available now we have to create the razor Pages for that I am going to select Pages inside these pages I will create a folder called product master inside this product master I am going to create Pages for the card operations right click on the product Master add go to the new scaffold item here we can select the razor Pages using Entity framework crud click on ADD here we have to select the required model class in our case product is our model class also we have to select the data context class we can select the FDB context is our data context click on ADD so it takes some time to create early pages to perform the current operations here we can see all our razor Pages has been created this is the index View and this is the code related to index view likewise delete View and delete related code here we don't have any controller we will be having CS file like waveforms if you have any experience in web forms it is similar to web forms so this is the view and this is the code behind file where inside the Cs file all the DB activities will be performed like this here if you want to get the product list here we are calling the geta sync method where we are going to connect with the context and we are going to read the data from the products table likewise if you go to the delete CS here we are going to get the data based on the ID and we are going to display it if there is no product available we are going to written as product not found so this is the post action method once product is available we are going to delete the product from the database we'll switch to details view so this is the view page and this is CS page so we are going to initialize our DB context here by using DB context here we are calling our product details based on the ID which is passed by the user this is the edit here we can edit the product based on the ID so this is the code where we are getting the data from the database based on the product ID if product ID is null or if product is not we are going to pause it as not phone if there is any product available then we are going to pause that product details here all the pages there is one bind property in our asp.net MVC applications we no need to use the bind property in controller we are going to assign the product list to the view here we are not going to use the View here we are not going to pass the model inside the return page here directly by using the bind property we can bind the data edit view here we are calling the edit model so this is the edit model by using the edit model here we are passing product details like product ID product name and the price quantity so everything we can access inside the view save this I am going to run the application now I'll call product it should be product master so this is our index page So currently we don't have any products so that's why we are not getting any data here so we will change this design I'll go to the index so I am going to make it as H3 after this I will add one horizontal row also for create button I am going to add bootstrap classes here before binding the data we have to check sometimes there will not be any data here so we may get null reference exception here so here we have to check if if it is not null and model Dot product dot if it contains any records then only it should come into this and it should bind the products so inside this Loop it will be executed for each Loop and it will bind each row also here this we can give header as action for the buttons like edit details and delete so if we don't have any details inside this product model so then we have to use the else condition here else here we have four columns so I use one table row table data I'm going to use the call span of four and class I will use text Center inside this I'll display as products not available in the database so this is a message which I am going to display to the user so whenever there is a product so it will be called inside this Loop and this will be executed and it will bind the data here if you don't have any data so then it comes to the else block it will display this message to the user will run the application once again so now this message is displayed now we can click on create new here we can create the products here also we can add some space after the quantity I'll change all the details then we can execute the application this also I'm going to make it as H3 here for the back to list also I am going to change class I'll keep this inside a div and I'll add class margin top two so then I'll go to edit edit also I'm going to change our design so by using scaffolding we now need to write any code so it will generate automatically so we can do required changes here so edit also we have the back to list here I am going to remove back to list here I'll move it to top here also I am going to use margin top two and after this button I am going to add two spaces and then I'll move it here here I can add Plus create also I can do few changes so we can make it as H3 instead of H1 here this back to list button I'm taking it to top so I can copy it from previous one so this is back to list in details I think we have not changed go to the details here I can add class Emoji top two class because BTN BTN primary and for back to list also I'll add on class hope we did all the changes here delete this is not report edit it's okay it's okay we will run the application now currently we don't have any products I'll click on create new so here I'm going to add some products here also we have to show space between this quantity and buttons watching top two create once the product is created it will redirect to the index page so I'll click on edit I can update the data here save so this product has been updated click on details we can see the product details back to list here we can delete the product once it is confirmed product has been deleted from the database click on create new so like this we can perform the crowd operations in asp.net core application by using the razor Pages there is no much differences between the MVC and the razor Pages if you know the net core MVC then you can easily understand this Ledger pages also there is no much differences here so this view will be similar to the dot net core MVC since we are using razor Pages here we have to use the at the rate page directive here and this is the reference of your CS file so this is the model called from here so in MVC application we will be using controller where all your DB activities will be performed inside the controller whereas in razor Pages we will be having View and CS file where CS file contains all our DB activities here we will initialize the DB context from there we can get the data and we can post the data to bind the data in the view so we will use the model property here this is our product model and we will use the bind property by using the bind property we can communicate with the view easily so to get the data and to post the data so we will use the bind property here so by using this bind property only so we can pause and we can retrieve the data here so this is the simple example which we have done in today's session in the next session we are going to perform same card operations without using any scaffolding mechanism we are going to so we are going to create each page manually and we are going to write all this code one by one so after that we are going to create another example related to crowd operations in asp.net core razor Pages we are going to use the stored procedures to perform the crud operations that's it for today if you like this video please like And subscribe to my channel thanks for watching
Info
Channel: CodeWithGopi
Views: 21,749
Rating: undefined out of 5
Keywords: .net core, asp net core razor pages, asp.net core razor pages, asp.net core razor pages crud, asp.net core razor pages example, asp.net razor pages, asp.net razor pages crud, asp.net razor pages tutorial, codewithgopi, crud, crud modal form in asp.net core razor pages, crud operations in asp.net core, razor, razor pages, razor pages crud, razor pages in asp.net core, razor pages in asp.net core in hindi, scaffolded razor pages in asp.net core, what is asp.net core razor pages
Id: 6Tm6QkdhDRo
Channel Id: undefined
Length: 27min 28sec (1648 seconds)
Published: Wed Aug 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.