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.

+1


source to share


3 answers


This has already been explained, but here is another suggestion on how to get what you are looking for:



if(searchBag.Qualities.SelectedValue != null){
    ListItem selected = (ListItem)searchBag.Qualities.SelectedValue.FirstOrDefault();
}

      

0


source


I'm assuming SelectList allows more than one item to be selected?

So SelectedValue is probably a list? Not 1 list.

Pass it:



 WhereListIterator<ListItem> selected = (WhereListIterator<ListItem>)searchBag.Qualities.SelectedValue;

      

instead and see what properties you have.

0


source


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?

0


source







All Articles