(#61) Get and save dropdown data in database | Asp.Net Core tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome again to this asp.net core M missile tutorial by now in this tutorial we have covered several things about rock town but the entire data that we were using into a drop down that was hard code now today in this video we will learn how to get and save the data or drove down to a database okay that's a start so we will get the data or drove down from the database and basically for that we need to create a table into a database so we will learn how to add a new table into our F distinct database using code first approach then we will learn how to save the data of the stroke down to the database so we are going to perform two equations over here first one is get the data second one is save the data so we are again back to our book store web application and as a first task we need to create a new entity into our code let's open this data folder and inside this data folder we need to create one new class so let's right click on this folder choose add and click on the class option let's do a language click on the add button ok let's add a few properties over here first first one is Heidi and this is going to be the primary key and the identity we can either use text or you can use name as well over here that's our one more property stating description perfect let's save all the changes now let's go back to our contacts class this contacts class is available over here inside this data folder and the name is bookstore context here you can notice that we have already added this books table in the same manner if we need to add our second table that is language that's that table as well so here we need to use D be set inside the DB set we need to provide the type of our table that is language now we need to give it a name so we can simply choose language over here ok so we have created a new table and we have added the reference of that table into our context class now we need to appear to the relationship in between these two tables so let's open the books class and insert this books class right now you can see first thing would be that we need to convert it to the integer because in this table we are going to store the ID of our language so how can we do that we need to simply use integer over here and we need to update all the references as well we need to upgrade the same property into our books model as well so let us open the models inside the book model you can notice that this is the language property and in this language we need to convert it to the integer perfect yes Billy the solution because the bill is filled cannot implicitly convert let's see what all we got over here the error is coming from this dummy data source and since we are working with the live data base now then there is no need of using this hard code data so I can simply remove this one okay so we do not have a search functionality right now I can simply return null and we will implement it later I just simply want to remove this data that is hard-coded and let's remove it from here in that started building this application this time you can notice that build is successful perfect now we need to go back to our books table and here we need to create a relationship between books table and the language table and how can we do that we simply need to write the property over here here as a type I need to provide the name of my second table that is language in twerkins let's give it a name and I can update this one as well by stating language ID you the bullet is not successful or what we have done is we have converted the language property to the integer type and we have made all the corresponding changes into my entire application okay now let's save all the changes and again go back to our books entity this is how you create a relationship in between two table this is public type and the name of your relationship if again you want to create the relationship from this language table to the books table then you can use the books table over here as well so you can simply use I collection and insert this I collection you need to pass the type of your books table that is books into our scenario and here we can write books now let's build the solution build is successful now it is the time to update the database and to update your database first we need to open the console window that is available over here in this tools new your package manager and here is a console first we need to add the migration let's give it a name click on the enter button a new migration has been added into our solution okay now it's time to update the Catalans to update the database you simply need to use one come on update database and press enter we are getting an error while updating the database why because as of now we are using language as n string but here we are using language as an integer so and we already have some data into our tables and because of the loss of that data we are getting this error so in real world application we will create a migration script and we will move that entire data from this string to the integer but now since you are working only on the demo application so you can simply remove the entire data from this books table and again I can add the entire data to this table let's open the database here is our books table let's get the data so right now I have only two books I can delete the entire data from this books table by using delete from books I am again telling you that in the real world application we you cannot delete the data you need to create any script and you will migrate the entire data from your stream type to the integer type in let's execute this query to those has been belittled from the database again let's try to run this command and it is done now let's go back to the database this time you can notice that we have two tables over here first one is books and second one is language let's see what all we got into this language let's right click on this one and choose design okay so we have three columns this one is hiding in data and we have this primary key as well and this is also the identity now let's see what all keys we go over here so we have one primary key and let's see what all we got into the books table so we have two kids over here into our books table this is the primary key and this is the foreign key this time we can say that our database is ready to perform some actions now let's add some data into a language table so right click on this one and choose edit here I can write some data just like we have added six language to our table let's close it and just to verify I can simply right-click on this one and choose select up thousand records here you can notice that here six language again we are back to our application and now it is time to work with the language table how can we do that we need to simply open this we post your folder inside this repository you can notice that we have one book the positive and since language is a separate entity so I need to create one more repository over here with name language you can do that easily head class language repository let's enter and all the common things that we have over here into our books equality I need to use all of them into our language property as well so I can simply copy this line let's get a constructor over here and let's redraw the dependency in the constructor let's use all the names pieces to perform the operations we need to create one more model into our application for language we already have a language model into our application and I simply need to update the text of these properties and the text is I can simply copy entire data from this this language table so I need to copy these three curves and I need to replace the entire code over here let's build the solution let's remove this hard code data as well because now we will get entire data flow map database again let's build the solution build is successful this time here we can write the code topic you think context door now we need to work with the language table so I can simply use this language and don't select I need to get the entire data in form of this language model so I can write language model over here and here I can map all the properties and get the last I can use to list a sync method okay let's snap all the properties so first we have ID howdy is equal to X dot ID description is equal to X dot scription and name is equals to X dot name perfect so I simply need to return this data let's return it by using this code we can get all the language from database now it's time to go back to the start of class and here if we have noticed that we are resolving this dependency by using this Eddy scope method we will learn the details of these methods in some upcoming videos but for now we need to resolve the same thing for our language diversity as well so I can do that language positive let's copy it again and paste it here perfect time to go to our controller class so here we are into the controller and here you can notice that you are using one book repository in the same manner we need to add the second repository as well angle is depositing and let's give it a name language repository and we need to resolve this dependency into the constructor let's build the solution just to test that everything is working fine for naught the build is successful now and here we are into a agni book X and Method and here if I want to get the language language is equal to then I can simply use this language suppose tree-toad get language and this method will return all the language that we have into a database so I need to convert it to an async method now we simply need to create a drop-down by using this language data and we have already learned several ways to create a drop down from this accent method so you can use any one of them okay let's pass this data into our view guide property let's ignore this data from here I need to perform the same things into our post method as well okay now let's go to our view if I need to get a drop down over here then there are two ways either I can use a select list over here or I can format my data into our controller and I can pass the formatted data over here onto my view let's format the data on to our controller let's format the data over here into our controller class so you can simply use new select list and let's see what all we go to into the Select list once again so into the constructor of the Select list we need to pass the data okay let's pass the data into a constructor so first one is the data and the second parameter is the we need to use this version so second second one is the data field data is available into the ID and the text is available into name let's make the changes into a post method as well now let's have a look on to this ugly book method as well so here you can notice that we are passing their language ID and the length is already perfect now let's run this application in debug mode okay let's see what we go into the top down so here we are getting all the languages that we have added into our database let's debug this code as well let's put a breakpoint over here into this get method this time and let's just refresh this page again so we got the breakpoint over here perfect let's continue and just to go insert this language you can click on the f12 button and let's put a breakpoint over here as well so we go to the request over here let's click on the Afghan button again click on the Afghan so we got all the languages over here into our view back property here you can see the items detail and these details are coming from the database and the ref thinks I mean creating the drop down from this data we have already covered that part several times in few previous videos so let's click on the continue button this time ok so we go to this language from the database let's choose the language let's add a new book title this time click on the author description total pages suppose I have 123 click on the add book button we are back to a post method and inside this post method here you can notice that into the language Eddie they are getting the ID of our Hindi language let's press f10 button the model is checked is valid in this scenario let's click on the earth clay one to go inside the exhibition of this method as well ok so here we are into the post method let's click on the f10 again click on the Afghan button and let's save all the changes perfect so the new book has been added into the database and the ID is 3 let's click on the continue button the new book has been added into the database successfully let's click on this here button to view the details of the book ok so over here you can notice the language is one why we are getting this one because as of now we are using language already property in the detail section as well so before updating this data let's focus on the database let's view the data of this books table let's click on this one and choose here you can notice that in the language property you can see that we have the ID of our language now we need to get the details of this bangle also and now we need to update the detail section of the book as well for that first view to open the books model inside this book model I need to create one more language property this is the ID property and I need to create one no language property that will hold the data of our language so in this time it will be string and language perfect now let's go to our get book data method and here it is into the books repository and we are using these two methods to get the details of all the so I can simply update the data in both of them now to get the data of this language there are two ways first one is either you can use the join or if you have created the relationship then you can simply use the navigation properties how book dot language and then dot ID so by using this approach you can get the details who know the ID the name by using this approach you can get the data from the related table let's make similar changes into the guidebook by already method as well poke note language dot name perfect now let's just build the application and run this application again click on the add new book again let's add one more book suppose this time I'm using this one click on the add book button book has been added successfully into the database click on the here link we are getting an exception over here it means we need to update some of the code as well let's update the code you now we are using the select method to slip the data again let's build the solution and just DIF this page again this time we'll notice that the error is gone but still you are having this idea - this is because we have not updated the property on towards your fight let's do that let's go to the view it is the book that book and over here instead of using this language ID I need to simply use this language property let's save the changes go back to the browser and refresh this page again and here you can notice that you are getting the text of our language so if we are adding the new book into the database then we are working with the ID and for the detail space we are using the text of the language that is all in this video thank you for watching heretic
Info
Channel: WebGentle
Views: 21,980
Rating: undefined out of 5
Keywords: asp.net core, asp.net core tutorial, asp.net core 3.0, asp.net core 3.0 tutorial, asp.net core mvc 3.1, asp.net core mvc 3.1 tutorial, asp net mvc, asp net mvc tutorial, dotnet core, dotnet core mvc, asp.net mvc core 3, asp.net mvc core tutorial, asp.net mvc core course, dot net core tutorial, dot net core mvc tutorial, asp.net core 3.1, .net core, get dropdown from database, dropdown with database, dynamic dropdown, save dropdown value to database
Id: zBJVaNp9euA
Channel Id: undefined
Length: 18min 50sec (1130 seconds)
Published: Mon Jun 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.