I'm storing a collection of List<KeyValuePair<string, bool>>
in ViewState, the problem occurs when i'm trying to cast it back to List<KeyValuePair<string, bool>>
Error states
Unable to cast object of type 'System.Collections.Generic.Dictionary
2[System.String,System.Boolean]' to type 'System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair`2[System.String,System.Boolean]]'.
So how can convert my key value list back from the ViewState ?
My code line is:
List<KeyValuePair<string, bool>> myObject = (List<KeyValuePair<string, bool>>)ViewState["listOutputWords"];
Try this:-
KeyValuePair<string, bool> myObject = (KeyValuePair<string, bool>)ViewState["listOutputWords"];
As clearly mentioned by the compiler, your ViewState is holding a generic Dictionary
of String,Bool
but you are trying to cast it to List of Dictionary which is wrong.
Based on the exception, you're saving a Dictionary<string, bool>
in ViewState
. If you really need a List<KeyValuePair<string, bool>>
, try:
List<KeyValuePair<string, bool>> myObject = ((Dictionary<string, bool>)ViewState["listOutputWords"]).ToList();
Or you could just deserialize it to a dictionary and use that:
Dictionary<string, bool> myObject = (Dictionary<string, bool>)ViewState["listOutputWords"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.