Hello Friends, In this tutorial we are going to learn different ways to Bind DropDownList in ASP.NET MVC. We have already familiar with ASP.NET MVC.
After Completing this tutorial you will be able to understand:
- How to Bind DropDownList in ASP.NET using different ways.
How to Bind DropDownList in ASP.NET MVC using different ways:
Let’s create an example for How to Bind DropDownList in ASP.NET MVC.
Step 1- Open Visual Studio and create an ASP.NET MVC application called BindDropDownListDemo.
Step 2- Choose an empty template and click OK. An Empty Solution will be created.
Step 3- Right Click on the Controllers folder and create a Controller called HomeController.
Example Of Binding DropDownList using Static Values.
To Bind DropDownList using static Values you just need to past List of SelectedListItem to DropDownList.
Step 4- Goto HomeController and create an Action method called BindStaticDropDownList.
Step 5- Right Click on BindStaticDropDownList Action method and create a View called BindStaticDropDownList. Write the following code in BindStaticDropDownList View.
Bind DropDownList with ViewBag:
We can also Bind DropdownList in ASP.NET MVC using ViewBag. For this create a List of SelectedListItem and Pass that List to ViewBag.
Step 6- Goto HomeController and write the following code.
Step 7- Right Click on BindDropDownListWithViewBag and Add a View with the name BindDropDownListWithViewBag. To Bind Values of ViewBag to the @Html.DropDownList just pass the ViewBag name to DropDownList. Add the following code in the View.
As you can see we have assigned the ProgrammingLanguages to @Html.DropDownList.
Bind DropDownList with Model Class:
We can also Bind DropDownList using Model Class. For this assign the List of SelectListItem to Model and pass that Model to View.
Step 8- Right Click on the Model folder and Add a Class called ProgrammingModel. Write the following code in ProgrammingModel class.
You can see in the above Model Class we have created a Property called lstLanguage of type List. This will hold the data and the second property specifies the Selected Index of DropDownList.
Step 9- Now open HomeController and Add the following Action method.
Bind DropDownList with Database:
In real time we need to deal with Database. So now will see how to Bind DropDownList using Database.
Step 1- Right Click on the Model folder and Create a Class called BindDropDownListRepository. Add the following method in the BindDropDownListRepository class.
Step 2- Now go to HomeController and Add an Action method called BindWithDatabase.
Step 3- Right Click on the BindWithDatabase and add a View with the same name. Write the following code in BindWithDatabase.
You can download the complete source code from here
Hope you understand this tutorial about How to Bind DropDownList in ASP.NET MVC in different ways. I will appreciate your Feedback, Comments and Suggestions.
- Bind Dropdown using Angular JS.
- ViewData VS ViewBag VS TempData in ASP.NET MVC.
- Generate Password using Stored Procedure in SQL Server.