How to set exist value as selected in dropdown list without hardcoding items in dropdownlist using mvc?

May 4, 2011 at 8:08 AM

Hi, i have form in mvc that retrieves records from database.this form have dropdowns. in Edit action of controller it is must to have selected the already inserted items. suppose if i am taking the User information for edit, then its City,State and country must be selected as they inserted before.Can any one tell me how to set values from database as selected in drop down list when page loads???

Oct 13, 2011 at 2:48 PM


Please create you IEnumerable object like this

ViewBag.City = new SelectList(your object,"CityId","CityName",SelectedValue);

and on UI bind it like this.


please correct spelling if i write wrong.