Cannot pass object of type WhereListIterator <system.web.Mvc.ListItem> for input system.web.mvc.listitem
I have a SelectList that I first check for the selected value! = Null and then I want to use that selected value in the where clause for the filter. For example:
if(searchBag.Qualities.SelectedValue != null){
ListItem selected = (ListItem)searchBag.Qualities.SelectedValue;
}
I did a toss in a separate, useless line to pinpoint the problem. It gives me
Unable to pass object of type 'WhereListIterator`1 [System.Web.Mvc.ListItem]' for input 'System.Web.Mvc.ListItem'.
Weuh?
- EDIT -
It was indeed because multiple choices were made. This was because on creation, I set the selected value to Items.Where (i => i.someCriterea) and I forgot to put .FirstOrDefault () at the end. The possibility of multiple responses is ending. Since it was IEnumerable, it was a lazy list and hence WhereListIterator I guess. I solved it by simply putting FirstOrDefault at the end.
source to share
SelectedValue is not a ListItem, it is the value of the selected list item. So, given this:
var selectList = new SelectList(companiesList, "Id", "Name", companiesList[3]);
selectList.SelectedValue will be equal to companiesList [3] .Id. If you want the actual list item to be able to do something like this:
ListItem selected = selectList.GetListItems().Where(x => x.Value == selectList.SelectedValue.ToString())
Just wondering why you need a selected list item?
source to share