model view controller - populating drop down box in mvc with list -
here controller code
public actionresult index() { list<dropdown> lobj = new list<dropdown>(); //datalayer.dboperation dob = new datalayer.dboperation(); data.db odb = new data.db(); dataset ds = new dataset(); ds = odb.loadgrid(); foreach (datarow dr in ds.tables[0].rows)//adding data list data set { lobj.add(new dropdown { data = dr["city"].tostring() }); viewbag.city = new selectlist(lobj); } return view(); }
and view page code
@html.dropdownlist("city")
its shows 6 times "mvc.models.dropdownlist" names in dropdown not show city names pls me in advance
it's because creating anonymous type not read view.
try this:
lobj.add(new selectlistitem { value = dr["city"].tostring(), text = dr["city"].tostring() });
(this classical problem, must duplicate, can't find exact match)
Comments
Post a Comment