Background
There are many ways to bind DropDownList in MVC. In this article we will learn how to bind simple dropdownlist using SelectListItem class. So let us learn in brief about SelectListItem class
What is SelectListItem Class
SelectListItem is a class which represents the selected item in an instance of the System.Web.Mvc.SelectList class.
It has the following properties.
- Text - It represent any text.
- Value - Its is the value against the particular text.
- Group - Group of items in single element .
- Disabled - Whether the selection item is disabled or not, default is false.
- Selected - Decides whether the item in list is by default selected or not, default is false.
Step 1 - Create an MVC application
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project" then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK.
- Choose MVC empty application option and click OK.
If you don't know how to create an MVC application, then please watch the following video.
Step 2 - Add controller class.
Right click on the Controller folder in the created MVC application and give the class name. I have given the class name Home and clicked OK.
HomeControlle.cs
- public class HomeController : Controller
- {
- // GET: Home
- public ActionResult Index()
- {
- List<SelectListItem> ObjItem = new List<SelectListItem>()
- {
- new SelectListItem {Text="Select",Value="0",Selected=true },
- new SelectListItem {Text="ASP.NET",Value="1" },
- new SelectListItem {Text="C#",Value="2"},
- new SelectListItem {Text="MVC",Value="3"},
- new SelectListItem {Text="SQL",Value="4" },
- };
- ViewBag.ListItem = ObjItem;
- return View();
- }
- }
Step 3 - Add View
Right click on the solution explorer of our created project and add the empty view. Now write the following code into the created view.
- @{
- ViewBag.Title = "Index";
- }
- <br /><br />
- <p>Dropdown Using viewbag with Html.DropDownList </p>
- <br />
- @Html.DropDownList("ListItem")
From all the above examples, we have learned how to bind the DropDownList in MVC.
Note
Summary
I hope this article is useful for all the readers. In my next article we will learn another technique to bind dropdownlist. If you have any suggestion please contact me.
- Download the Zip file of the sample application for a better understanding.
- You can bind the list from the database, since this is an example so it did not used any database.
- Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
- Handle the exception in the database or text file as per you convenience, since in this article I have not implemented it.
I hope this article is useful for all the readers. In my next article we will learn another technique to bind dropdownlist. If you have any suggestion please contact me.


sanjay pariyarPosted Apr 5, 2018, 10:57 PM EDT
Please same dropdownvalues in database
Vithal WadjeTOP 100Posted Oct 23, 2015, 8:48 PM EDT
Thanks
Ekrem TapanPosted Oct 23, 2015, 12:45 AM EDT
thanks for tutorial
Vithal WadjeTOP 100Posted Oct 18, 2015, 1:26 AM EDT
Thanks sir
Santhakumar MunuswamyPosted Oct 17, 2015, 10:45 PM EDT
Good one
Vithal WadjeTOP 100Posted Oct 15, 2015, 10:17 PM EDT
Thanks
Shridhar SharmaPosted Oct 15, 2015, 11:50 AM EDT
nice share sir :)
Vithal WadjeTOP 100Posted Oct 15, 2015, 10:03 AM EDT
thanks
Nithya MohanrajhPosted Oct 15, 2015, 3:31 AM EDT
Neat and Simple.Thanks.
Vithal WadjeTOP 100Posted Oct 15, 2015, 3:31 AM EDT
Thanks Atul Rawat sir
Rupali ShindePosted Oct 15, 2015, 3:09 AM EDT
nice
Atul RawatPosted Oct 15, 2015, 2:44 AM EDT
nice article sir
Vithal WadjeTOP 100Posted Oct 15, 2015, 1:27 AM EDT
Thanks Sibeesh Venu sir
Vithal WadjeTOP 100Posted Oct 15, 2015, 1:27 AM EDT
Thanks Rajeesh Menoth sir
Sibeesh VenuTOP 100Posted Oct 15, 2015, 12:13 AM EDT
Thanks for sharing
Rajeesh MenothPosted Oct 14, 2015, 11:28 PM EDT
Good One..!!
Vithal WadjeTOP 100Posted Oct 14, 2015, 10:52 PM EDT
Thanks Mukesh Kumar sir
Vithal WadjeTOP 100Posted Oct 14, 2015, 10:51 PM EDT
Thanks Nilesh Jadav sir
Vithal WadjeTOP 100Posted Oct 14, 2015, 10:50 PM EDT
Thanks Harshad Pansuriya sir
Mukesh KumarPosted Oct 14, 2015, 10:28 PM EDT
Great JOB
Ankit BansalPosted Oct 14, 2015, 8:48 PM EDT
nice..thanks for sharing
Nilesh JadavPosted Oct 14, 2015, 8:45 PM EDT
GOod work sir !
Humayun Kabir MamunPosted Oct 14, 2015, 8:03 PM EDT
Nice...
Harshad PansuriyaPosted Oct 14, 2015, 8:01 PM EDT
nice one