Click here to Skip to main content
15,920,030 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to add data in dropdown list from database using MVC.please help me out?
I have add dropdown in my asp page now i want to populate data in it coming from database.
Posted
Comments
[no name] 4-Jul-12 10:02am    
And what exactly have you tried?

1 solution

You have to pass a SelectList instance to the view. You can either add it to the viewbag (or to the viewmodel) like this:
C#
ViewBag.DropDown = new SelectList(IEnumerable of elements to be listed in the dropdown, "element field to be used as index", "element field to be used as display text")};


Than you can use it in your view:
C#
@Html.DropDownList("DropDown")


Of course consult MSDN for other features:
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist.aspx[^]
http://msdn.microsoft.com/en-us/library/system.web.webpages.html.htmlhelper.dropdownlist(v=vs.99).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900