asp.net mvc - Set selected item of DropDownListFor in view -


in asp mvc site want able select default drop down list item in view. however, overload i'm using seems add item drop down list instead of specifying 1 want selected.

here code in controller created selectlist

selectlist tmplist = new selectlist(new[] { "", "saf", "sny" }); viewbag.companies = tmplist; 

and here code in view. this, however, winds adding saf list screen shot below shows.

@html.dropdownlistfor(model => model.agencycompany1, (selectlist)viewbag.companies, "saf") 

enter image description here

you can try in controller, second "saf" setting selected object:

viewbag.list = new selectlist(new[] { "", "saf", "sny" }, "saf"); 

and in view (you don't need last "saf" parameter anymore):

@html.dropdownlistfor(model => model.agencycompany1, (selectlist)viewbag.companies) 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -