Add Where Condition For Dropdown List

I have a simple checkbox that fills my bag like:

 ViewBag.stuId_FK = new SelectList(db.CLS_Students, "stuId", "student");

It works great. What I'm trying to do is filter the conditions using the where clause (an example where my "position" field is 1).

I have this code, but I don’t think it is for sure.

ViewBag.stuId_FK = new SelectList(db.CLS_Students, "stuId", "student").Where(o=>o.positionID==1);

Any help would be appreciated. Thank.

+4
source share
1 answer

Try filtering the collection before creating an instance of SelectList.

Like this:

ViewBag.stuId_FK = new SelectList(db.CLS_Students.Where(o=>o.positionID==1), "stuId", "student");

By doing this, you filter the data on your model instead of displaying the entire table in memory and then filtering.

+7
source

Source: https://habr.com/ru/post/1650342/


All Articles