James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

MVC - Order DropDown List

10:00 PM
Sometimes the data in a table is not ordered in a fashion you want and you can't change the database.  Here is an easy way to order the items from within your controller:

ViewBag.cities= new SelectList(db.cities.Where(g => g.visible == true).OrderBy(g => g.name), "id", "name", myitem.cityId);

or

ViewBag.cities= new SelectList(db.cities.OrderBy(g => g.name), "id", "name");

0 comments:

 
Toggle Footer
Top