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")
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
Post a Comment