Introduction
Cascading Dropdown is also known as Dependent Dropdown.

If you are new to .NET Core, read these articles to kick start your .NET Core knowledge.
- Creating A New .NET Core Web Application Using Visual Studio 2015
- Creating A New .NET Core Web Application With Command Line Interface (CLI)
- Setting And Reading Values From "appsettings.json" In .NET Core
- Binding Dropdown List With Database In ASP.NET Core MVC
Topic
- Database part
- Creating an application
- Installing Package for Entity Framework Core From NuGet
- Adding Connection string and Setting up DbContext
- Adding Category, SubCategory, MainProduct Model in Models Folder
- Adding DbSet for Category, SubCategory, and MainProduct Model in DatabaseContext class
- Adding Controller [DemoController]
- Getting Data from the Database using Entity Framework core.
- Adding View [index. cshtml]
- Binding Category Dropdownlist using new Tag helper
- Adding SubCategory and Product Dropdownlist to index.cshtml View
- Adding SubCategory and GetProducts Action Method to Demo Controller
- Adding JQuery reference on View and Default items in a drop-down list
- Binding SubCategory and Product drop-down list with jQuery Ajax
- Adding Index Action method for handling post requests and getting selected values
- Saving and Run Application.
Step 1. Database part
I created a sample database with the name “AllSampleCode” for showing the demo.

Inside this database, I have added 3 tables - Category, SubCategory, and MainProduct. You can see the structure of the tables below.



Step 2. Creating the application
Now, let’s create a .NET Core Web application.
Open Visual Studio IDE from the start page and click on the New Project link.

It will open a new dialog with the name “New Project”. From the left pane, choose Templates >> Visual C# >> .NET Core template. Then in the Middle pane, you will see .NET Core project templates. Choose “ASP.NET Core Web Application (.NET Core)” project templates.

After choosing the project template, next, we are going to name the project “MVCCore1” and finally, we will click on the OK button to create a project. But, it will pop up another dialog with the name “New ASP.NET Core Web Application (.NET Core)”.

Inside this dialog, we are going to choose the “Web application” project template for creating “Web application” and click on the OK button.
Below is the complete project view that is newly created.

After creating the application, next, we have to add the references needed for Entity Framework Core.
Step 3. Installing package for Entity framework core from NuGet
To install the package, just right click on the project (MVCCore1) and then select the Manage NuGet package. The below dialog of the NuGet Package Manager will pop up.
In the browse tab, type “Microsoft.EntityFrameworkCore.SqlServer” in the search box and just click on the Install button.

Microsoft.EntityFrameworkCore.SqlServer
Step 4. Adding Connection string and Setting up DbContext
After adding a reference, now add a connection string in the appsetting.json file.

After adding a connection string, the next step is to add a class that will inherit the DbContext class. Before doing this, let's start with creating a folder for models, and inside that, we are going to add this class.
For adding a folder, just right click on the project (MVCCore1), then choose Add from the menu that pops up, and inside that choose New Folder.
Add New Folder.

Now, let’s add a class with the name DatabaseContext in the Models folder.
To add a model, just right-click on the Models folder. Then, select Add >> Class. An "Add New Item" dialog will pop up with the default class selected. Name the class as DatabaseContext and click on the Add button.

After adding a DatabaseContext class, next, we are going to inherit the DbContext class.
After inheriting with DbContext, next, we are creating a constructor which takes DbContextOptions as an input parameter and also inherits the base class constructor (: base(options)) [DbContext].

Next, we are going to add a new Service in Startup.cs class for injecting dependency.
Now, whenever you use the DatabaseContext class, the DbContext instance will be injected there.

Step 5. Adding Category, SubCategory, MainProduct Model in Models Folder
For adding a model, just right click on the Models folder, and select Add >> Class. The "Add New Item" dialog will pop up with the default class selected. Name the class as Category and click on the Add button.
Adding Model Category.

Adding Model SubCategory.

Adding Model MainProduct.

After adding Category, SubCategory, and MainProduct Model, in our next step, we are going to add the DbSet of all models in the DatabaseContext class.
Step 6. Adding DbSet for Category, SubCategory, and MainProduct Model in DatabaseContext class
Now, let's add DbSet for Category, SubCategory, and MainProduct Model in the DatabaseContext class, as shown below.

Step 7. Adding Controller [DemoController]
To add a Controller, just right-click on the Controller folder, and select Add >> New Item. After selecting New Item, a new dialog of Add New Item will pop up.
Inside that, just choose “MVC Controller Class”. Name the Controller as “DemoController” and click on the Add button.

After we have clicked on the Add button, it has created DemoController in the Controller folder, as shown in the below view.

Step 8. Getting Data from the Database using Entity Framework core.
In DemoController, we are using constructor injection for getting a dependency.

Step 9. Adding View [index. cshtml].
To add View, just right-click on the Views folder, and then Add >> New Folder. Name the folder as “Demo”.

After adding a Demo folder, now we are going to add View inside this folder.
For adding View, right-click on the Demo folder and select Add >> New Item. A new dialog "Add New Item" will pop up.

Just choose “MVC View Page” to add View and give a name to the View. The View name must be the same as the action method name. So, we are going to name it “Index” [“Index.cshtml”] and click on Add.

Step 10. Binding Category Dropdownlist using a new Tag helper.

After binding the first drop-down list of Category on the index.cshtml View, now we are saving the application and running it. Access the URL. http://localhost:####/demo/index

Now, we have added only one drop-down list on the index.cshtml view. Let’s add the remaining 2 drop-down lists also.
Step 11. Adding SubCategory and Product Dropdownlist on index.cshtml View

After adding all drop-down lists on View, now let’s run the application and check how our View looks.

We can see all the drop-down lists above but only one drop-down list is filled with data while the remaining two are empty. For filling data in this drop-down list, we need to use jQuery AJAX.
Step 12. Adding GetSubCategory and GetProducts Action Method to Demo Controller
Also, we need to provide the data to the dropdown list in JSON format because we are using jQuery AJAX for binding this dropdown list. Doing that, we need to add two action methods.
- GetSubCategory(int CategoryID): Bypassing CategoryID to this method, it will return all Subcategories [will return data in JSON format]
- GetProducts(int SubCategoryID): Bypassing SubCategoryID to this method, it will return all Products [will return data in JSON format]
Adding GetSubcategories Method to Demo Controller.

Adding GetProducts Method to Demo Controller.

After adding action methods, we need to add jQuery AJAX code in the Index.cshtml view to call these Action Methods.
// GetSubCategory method will get called on change of the Category drop-down list
// GetProducts method will get called on change of the SubCategory drop-down list
Step 13. Adding jQuery reference on View and default items in a drop-down list
Adding jQuery reference on View.

Adding Default items in a drop-down list.

Now, if you run the application, you will not see an empty drop-down list.

After adding default items in the drop-down lists, now let’s bind SubCategory and Product drop-down lists.
Step 14. Binding SubCategory and Product dropdown list with jQuery AJAX
Now, if we look at the below code, in the document ready function, we have written the change function of the Category drop-down list. In that, we have first declared the URL for getting data of SubCategory, then we have used $.getJSON method to call the GetSubCategory action method and along with that, we are passing the parameter CategoryID to this method. In return, we get the collection of SubCategory List which we are going to iterate and bind to options of the SubCategory dropdown list.
In the same way, we have written the change function of SubCategoryID dropdown list. In that, we have first declared the URL for getting the product data, then we have used $.getJSON method to call the GetProducts action method and along with that, we are passing parameter SubCategoryID to this method. In return, we get the collection of Product List which we are going to iterate and bind to options of product drop-down list.
Below is the complete code snippet of binding SubCategory and Product drop-down list

Now, if you save and run the application, then access the URL: - http://localhost:####/demo/index

Wow! We have populated all the dropdown lists.
Step 15. Adding Index Action method for handling post requests and getting selected values
For handling HTTP post requests, I have added a new action method that takes the Category model as an input parameter. When we are going to post an index form, then the Category model will populate with selected values of the drop-down list that users have selected.
In the below snapshot, you can have a look at how selected values are populated in the Category model.

Step 16. Saving and Running the Application.
Now, after saving and running an application, access the URL. http://localhost:####/demo/index.



- First, we are going to test Validation.
- Choose Value from the Drop-down List and submit.
- Debug values after submitting the form.
Note. Download the entire source code of this application along with the database script and try making your own demo to learn more.
Finally, we have completed the "Creating Simple Cascading Dropdown List in ASP.NET Core MVC". Thanks for reading the article. If you like it, please share it.

Udr SerPosted Oct 29, 2022, 3:20 PM
The DatabaseQuery1 actually contains a query to create some countries, it is not the right query
Richard LobaPosted May 2, 2022, 10:31 PM
This was a clean step by step; however, I was Stopped at step 10. details below. I downloaded the rar you have, and it was unable to be loaded into VS 2020. I tried the downloaded version in 2017 and it failed. Do you have source to a version that will run under VS 2020?: Step 10 error An unhandled exception occurred while processing the request.InvalidOperationException: Could not create an instance of type 'MVCCore1.Models.DatabaseContext'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, give the '_context' parameter a non-null default value. Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexObjectModelBinder.CreateModel(ModelBindingContext bindingContext)
kt wongPosted Dec 14, 2021, 4:32 AM
May I know how to reset the Category to Select after posting the page?
Halber AlbarracínPosted Apr 26, 2021, 7:45 PM
Thank. I really need to understand this topic and this example is very nice
Ali SadeghiPosted Jan 18, 2021, 10:44 AM
Hi Saineshwar Bageri there is a problem with the solution incompatible error occur when opening in visual studio
Peter EgglesonPosted Jan 16, 2021, 1:43 AM
Great solution, but the SQL in the DatabaseQuery1.rar download does not match the AllSampleCode tables referenced in the solution.
Marius VasilePosted Dec 19, 2020, 3:38 PM
Cascading dropdownlist for razor pages on asp.net core here https://www.c-sharpcorner.com/forums/asp-net-core-razor-pages-cascade-dropdownlists
Cop BlasterPosted Oct 21, 2020, 10:16 PM
Do you have an example that works on a Razor Page? I've tried modifying this but it always says that items is null
Roberto LondonoPosted Jul 15, 2020, 4:05 PM
Hi, Saineshwar I appreciate a lot your excellent article.
Arif AliPosted Jun 30, 2020, 4:34 AM
Hello sir this is arif from delhi please you suggestion me for asp.net ,core and mvc its ded or not ,i am very confuse
George CokerPosted Jun 17, 2020, 4:49 PM
Thanks for the solution. The project version is no longer supported.
MePosted May 3, 2020, 9:29 PM
This is atrociously complicated. All that code for 3 selects. Microsoft hugely missed the mark on this and it's only gotten much worse in Core.
Iovanni Fernández SánchezPosted Apr 2, 2020, 2:02 PM
Thanks, it's a great solution
Tiago LopesPosted Mar 9, 2020, 5:31 AM
Very Useful thank you
Tony TrappPosted Feb 21, 2020, 9:37 AM
Hi there Saineshwar I got the results on my second drop list but I have a new problem all the items says its all undefined and tried everything...I am at my wits end....please help....thanks
João GonçalvesPosted Oct 17, 2019, 1:47 PM
Thank you very much.
João GonçalvesPosted Oct 17, 2019, 1:47 PM
You saved my life!
Vikas VikasPosted Sep 15, 2019, 6:29 AM
Hello Sir, I have used this method and successfully create cascading dropdown with 4 categories. But now the problem is I have a form where the customer fills information like name, age, and choose a category, subcategory, product & product items but there I get the problem. so please can you help me if I submit you full source code & scripts where i do mistake from my end?
Tony TrappPosted Sep 10, 2019, 8:56 AM
Hi Saineshwar I am using asp.net core 2.2 MVC I have a problem, i get two dropdowns, the first one works just fine but the second only shows one item instead of 3, what am I doing wrong?
Sarmad YousifPosted Aug 14, 2019, 3:40 PM
May i have the full project because i can't download it. thanks
hari rajanalaPosted Jul 21, 2019, 8:02 AM
Hi sir,How we call the selected values from database in jquery code and in controller
jordi estradaPosted Jun 27, 2019, 11:58 AM
HI, when i validate the form the dropdowns lost the data, how to fix that?
wayne bPosted May 10, 2019, 4:32 PM
It seems like the DatabaseQuery1.rar file is not the correct SQL for this tutorial. The one linked above creates the table "CountryMaster". Thanks, enjoying your tutorial.
بارآسا بارآساPosted Jan 26, 2018, 1:23 PM
DropDownList Razor Page?
Luis Alberto Delgado de la FlorPosted Jul 14, 2017, 4:01 PM
Hello, Saineshwar! I have a question regarding your post but in the case that we have a model that uses this cascading dropdownlist to save some of its properties but the problem comes when we want to update that model and repopulate the dropdownlists with the corresponding info. Here are the details in case you can check it sometime! Thanks! http://www.c-sharpcorner.com/forums/cascading-dropdownlist-updating-an-existing-value
Luis Alberto Delgado de la FlorPosted Jun 22, 2017, 5:34 AM
Hello! Thanks for the contribution! I have a question: Suposse we have a form where we add new products and we want to use this cascading dropdownlist for the Category and Subcategory atributes in the form used to add these new products, what model shall we use in this Create product page? I'm guessing we would need a new model that includes the Product (in order to get all the attributes of the new product) and the Category (for the cascading dropdownlist to work) together, correct?
HowardPosted Feb 4, 2017, 8:34 AM
Noted when setting data back to ViewBag after posting form, the Category dropdown does not update/refresh to display CategoryName as 'Select'. How can this be achieved?
HowardPosted Feb 3, 2017, 10:10 PM
Great article and easy to follow the logic.
sreenivasa kPosted Dec 14, 2016, 2:10 AM
Excellent article and nice worthy