Click here to Skip to main content
15,896,201 members
Articles / Web Development / ASP.NET

ASP.NET MVC - Help needed to Improve the code for mvc's Drop-Down Box?

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Jan 2013CPOL 0  
Create a class DropdownList where T is the entity which contain the property for value and text and method like belowpublic static class DropDownList { public static SelectList LoadItems(IList collection, string value, string text) { return new...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
16 Jan 2013Sheikh Muhammad Haris
Hi,Try the following code [HttpGet]public ActionResult Index(){ ViewBag.CitiesDropDown = GetCityList(); return View();}private List GetCityList(){ //Here you can access DB to get your required data. Just make sure the return type ...
Please Sign up or sign in to vote.
16 Jan 2013devdev13 3 alternatives  
I have created the below drop-down to populate a list of cities.All works fine, but I would like to know the better ways of doing this please. Also, please let me know if it is possible to create the same drop down using instead of HTML helpers.Below are my DTO classes. Please...
Please Sign up or sign in to vote.
16 Jan 2013Zoltán Zörgő
You have several ways to do this. You don't even need to use ViewBag. Please look at my recent article: Dynamic tabular input with unobstructive validation in ASP.NET MVC3[^] (:)). It covers this scenario also.SelectListItem is useful if you generate your element on the fly, Selected has to...

License

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


Written By
Software Developer (Senior) Orion India Systems
United States United States
Software Engineer At Orion System Integrators,New Jersey,United States Of America and have an experience of more than 4 years in Microsoft Technologies.

Article of the Day on Microsoft's site http://www.asp.net/community/articles on Friday, September 20, 2013.
My Blog :https://fromjami.com/

Comments and Discussions