문제

I got compiling error below:

Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string'

Argument 3: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string'

How to fix this error?

void Main()
{
    SortedDictionary<int, string> items = new SortedDictionary<int, string>{{1, "apple"}, {2, "book"}, {3, "tree"}, {4, "mazagine"}, {5, "orange"}};
    MultiSelectList msl = new MultiSelectList(items, items.Select(o => o.Key), items.Select(o => o.Value), items.Where(i => i.Key == 1 || i.Key == 5)).Dump();
}
도움이 되었습니까?

해결책

MultiSelectList(items, dataValueField, dataTextField, selectedValues) constructor expects four parameters:

  • items collection - you are passing them correctly
  • dataValueField string - here is your first error. You should pass name of value field instead of passing all values.
  • dataTextField string - here is your second error. You should pass name of text field instead of passing all text values.
  • selectedValues collection - here is your third error. You should pass values only (that is 1 and 5 in your case) instead of passing items which have selected values.

So, correct code is

new MultiSelectList(items, "Key", "Value", new [] { 1, 5 })

I suggest you to read carefully error messages, look on IntelliSense hints, and use MSDN to get information about types you are using.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top