(#60) Create dropdown using Enum in asp.net core | Asp.Net Core tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will learn one more we took it or drop down in asp.net core here we will create the drop down by using n enough we need to create a drop down by using enum in asp.net core let's open the visual studio in few previous videos we have covered several ways to create a drop down in a spirit net code if you want to learn more about the drop downs then you can watch the previous videos of this tutorial and I hope you will be able to understand everything about the drop down in asp.net core okay now here we need to create a drop down by using n in a first let's create an enum over here in this application and for that let's add a new folder first younam's and let's start a new innum over here let's give it a name language you know let's update this class to the enum let's add a few properties over here so suppose I am writing some language which one is Hindi suppose this is the enum that I want to use for the drop down in asp.net core let's go back to the controller class since we are using an enum to create the drop down property so we do not need this data over here we can simply current this data all together let's command same thing into the post method as well okay now let's open the model class insert this book model you can notice that we are using this multi language in the previous video of the tutorial we have covered multi select value from this drop-down we will make some changes in this existing property and now we will get the letter in form of an enum so let's update this type to an enum the name of our enemies language Hinnom we can update the text of this property as well suppose now on to it language you know is it just for the identification that you are using in an over here in general you will have a normal language property and the type of that language will be langostino but here we have already used this language and there are multiple reference of this language that is why I am not making anything in this existing property just for the demonstration purpose I am creating a new property over here perfect okay now let's use this language enum onto our view file let's go to the view to open the view you can simply right click on to your Epson method and click go to view this is the code that we are using to create a drop down into our view file here we need to update the name of our property instead of using this multi-language we can simply use language in um okay if you have noticed that we are using ASP items over here and in this ESP items we are passing the data in form of view back dot language since we do not have any view bellroad language data now so we can remove this form together and we need to gather the data from an e now in asp.net core if you want to work with the enum then we already have an inbuilt method so we can access that method by using HTML node get in a select list insert this get in on select list we need to pass the name of our enum and the name the enemy is linguist you know so we can pass it like this now you can see that we do not have the reference of this language in um over here in this view so there are two ways first one is you can use a namespace over here for the second one is you can write the enter data into our view imports file since I want to access this in am only on the single page so I can write my code over here in this view so the name is space for this particular in amis we have dental door bookstore door enums so I can write it over here like this and this time you will notice that we are able to access this language in um over here let's use this method and the error is gone let's have a look on this get in am select list method right click on this one and choose go to definition this method is available in Microsoft or ESP net code dot me0 trend the return type of this get animalist method is select list item so ultimately we will be getting a collection of select list item from this get in am select list method and we have already covered this select list item in previous videos of this tutorial if you are getting this left list item from our inner then we will be able to create this drop-down automatically into our application let's remove this multiple flow here if you want to add the multiple then you can use this one as well okay so we do not need to make any further change let's turn this application in debug more now let's click on the add new book link again and this time you will notice that we are having all the values from our inner let's have a look on the HTML as well in the HTML you can see that by default we are getting few values like Hindi is zero English is one it means if you are not passing any value to our enum then by default the value will be start from 0 and 0 1 2 3 you will have all the values in this order let's select a value suppose I want to select English let's click on the add book here we are in the post method and since we are using this language enum here you can see that we are having this English value and it is coming from the enum let's continue perfect it is working fine now let's learn how can we add some other values to are enough here if you want to assign some other value to your language then you can do that easily suppose you want to assign 10 to this Hindi 11 let's run this application again so we have the drop down into our form let's open the HTML this time you will notice that in the value property we are having values 10 11 12 13 and 14 so this is how you can update the values of your properties in ena now what will you do if you want to update this text as well to update the text if you simply need to use the display attribute that we are already using into our model so we can use this display over here and this display is available in system load component model dot data annotations if you will use the display over here the reference will be added automatically to your application here we can use the name property and insert this name property we can pass the custom name of this property suppose here am writing let's do the same to all the values there is no need to make any change into your view file let's run this application in debug mode now click on the add new book again this time you will notice that the text has been updated of your drop-down here are the values in the language English language and these values are coming from the display attribute that we have defined onto a ena let's open the HTML of the stroke down here you can notice that the text has been updated but the value is still same now let's select a value and let's see what will happen into the post method let's choose a value from this drop down suppose I am using this German language click on the add book over here in this Porsche method you can see that still you are having this German over here in this language in them so this is how you can add custom values to your inner you can update the text of your inner properties and everything will work as expected if you will not select any value over here then you will get n validation error as well let's click on the continue button this time and we are getting this validation error in few previous videos we have covered lots of ways to create this drop down in asp.net core I hope after watching this video you will be able to create the drop down in any situation in any issue wrote net core web application make sure you hit the like button of this video if you have any questions or queries or solutions then you can ask me into the comment section of this video see this video subscribe to the channel
Info
Channel: WebGentle
Views: 16,818
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, Create dropdown using Enum, Create dropdown using Enum in asp.net core, enum dropdown
Id: 0JDxnjcH_v0
Channel Id: undefined
Length: 8min 1sec (481 seconds)
Published: Mon May 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.