c# - mvc 4 drop down default value selected -


i want select default value in drop down list policyid = 7 didn't select value, doing wrong?

controller:

var pm = new managerclass(); ienumerable<myclass> po = pm.getdatafromdb(); ienumerable<selectlistitem> policies = new selectlist(po, "policyid", "policyname", new { policyid = 7 }); viewbag.policies = policies; 

view:

@html.dropdownlistfor(m => m.policyid, viewbag.policies ienumerable<selectlistitem>, new { @class = "dropdown-field"}) 

just set policyid property on view model value want preselected:

var pm = new managerclass(); var po = pm.getdatafromdb(); viewbag.policies = new selectlist(po, "policyid", "policyname");  viewmodel.policyid = 7; return view(viewmodel); 

since dropdownlist bound policyid property (m => m.policyid), value used when deciding element preselected.


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 -