James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

MVC - Filter DropDown List in With Where Clause

9:30 PM
Here is a super-simple way of filtering the content of a dropdown list populated by a controller.  For example, say this is your primary table of selectable items.  However, you want to exclude items that are not marked as "visible". 

id title visible
1 San Diego, CA true
2 Los Angeles, CA true
3 Austin, TX false

DropDown List (razor format)
@Html.cities("id", String.Empty)

Original:
ViewBag.cities= new SelectList(db.cities, "id", "title", myitem.cityId);

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

0 comments:

 
Toggle Footer
Top