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

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -