I have a simple checkbox which is populated by my viewbag like this:
ViewBag.stuId_FK = new SelectList(db.CLS_Students, "stuId", "student");
This works fine. What I am however trying to accomplish is, filter the conditions on by enforcing a where clause(example where my field "position" is 1).
I have this code but, I dont think this is accurate.
ViewBag.stuId_FK = new SelectList(db.CLS_Students, "stuId", "student").Where(o=>o.positionID==1);
Any help would be appreciated.Thank you
Try filtering the collection before instantiating a SelectList.
Like this:
ViewBag.stuId_FK = new SelectList(db.CLS_Students.Where(o=>o.positionID==1), "stuId", "student");
Doing this, you are filtering the data on your model instead of mapping the entire table on the memory and then filtering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With