Part 52. Dropdown using ENUM in .NET Core MVC. | Convert ENUM to anonymous object. | ASPNETCOREMVC.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to programming concepts my name is amit and this is part 52 of asp.net core mvc tutorial in this video we will learn how to create a drop down with the help of enum in.net core mbc this is a continuation of part 51 drop down using multi select list in dot net core mvc so please watch it before proceeding with this one i shared the link in the description in our previous videos we have created a drop down with the help of option tag select list item select list and multi select list in this video we will explore how to create a drop down with enum let's get back to visual studio and this is the project which we are working on let's quickly create an enum within the controller class for demo purposes again disclaimer this is only for demo purposes that we are creating such classes and enum within the controller class all right at the top type public enum depts with capital d for department curly braces select department comma eng comma hr comma pm now we need space within select department like select space department and we know enum will not allow us just place space and you can see your reds quickly see error to overcome this we can use display attribute so type display within square brackets press ctrl dot and it is present under system.componentmodel.data annotations class add the namespace now within bracket use property name and is equal to select space departments next let's go to our partial view within asp hyphen items delete our view back and use html dot get enum select list let's go to its definition and if you look at its summary and parameter you can see it accepts enum and will return select list and in part 50 of this video series we already seen what select list is and how to play with it all right let's go to our partial view and provide our department enum to html.getenumselectlist method delete multiple keyword we don't need it and we already know it is a part of select element so behavior will be the same save and run the program click on add employee you can see a drop down is created right click and inspect the element and you can see enum by default provide value in a sequential manner starting from 0. if you want to explicitly specify the value you can specify that in the enum let's get back to visual studio stop the application within controller let's say the department value is zero engineering value is one hr value is 2 and pm value is 3. save and run the program click on add employ drop down is created right click and inspect the element expand select list and you can see the correct value is associated with the text now because enum is also a key value combination or key key combination you won't get the luxury to associate other properties to it which you can use to apply other features like disable element and grouping we need to do modifications at c sharp level as we discussed in part 50 of this video series where we converted select list item to select list again that won't make any sense to use enum if you want to use all these features i searched through the internet but haven't found any efficient way to do this just in case anybody is interested in how to hard code stuff with an enum disable and create the group let's cover that part as well so within our employee controller add employee get method let's delete this dictionary that we had created for demo purposes also delete selected values and selected list let's type where depts for departments is equal to now we will apply some link you so from depts our enum let's name it e for enum in enum.getvalues we will be going to use this method getvalues of enum and let's pass the type of our enum name which is dbts next let's apply a select query to it so select new curly braces semicolon let's define anonymous properties to this anonymous object let's say id is equal to now we need enum value so typecast enum variable e to end next we need string value of enum which we'll be going to use as a drop down text so name is equal to e dot tostring next we need to demo grouping as of now we have nothing where we can identify which value belongs to which group right so let's assume a department having an integer id greater than 2 belongs to group b and department having an id less than or equal to 2 belongs to the group a so group is equal to typecast enum so end e greater than 2 question mark if it is greater than 2 then group b else group a simple we just apply ternary operator to define which department belongs to which group next create an object of select list so select list let's name it select lists which small s is equal to new select list pass our newly created local variable depts comma its value field will be ideal next it's text field will be name and let's say by default we want to select a department with id is equal to one and finally its group field is group this variable is already associated with our view back so use this view back in our partial view and pass it to our asp hyphen items all right save and run the program click on add employee and you can see we have drop down created eng is selected by default expand and you will see group is also created right click to inspect the element expand select items and you can see correct values are associated with the text simple extremely simple again this example is only to fulfill your curiosity if you have such requirement where you have to use all these features it's good to go for select list or select list items all right then that's it in this video if you have any queries related to the content of this video do ask me in the comments till then thanks for watching [Music]
Info
Channel: PRO Concepts
Views: 899
Rating: undefined out of 5
Keywords: asp.net core, asp.net core tutorial, asp net mvc tutorial, dotnet core, dotnet core mvc, asp.net mvc core tutorial, asp.net mvc core course, dot net core tutorial, dot net core mvc tutorial, .net core, Dropdown in asp.net core, Dropdown in asp.net core using ENUM, dropdown in asp.net core mvc, drop down list asp.net core, dropdown using enum in core, drop down list in asp.net core, Pro Concepts, Amit Singh Rawat, dropdownlist enum, .net core tutorial
Id: CnBxGYW3FvA
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Thu Sep 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.