Skip to content Skip to sidebar Skip to footer

Returning Dictionary As JSON And Successfully Accessing It's Contents Withing JS Function - C#

I have a Dictionary named finalDictionary which is created by consolidating multiple Dictionaries. This is the piece of code which I use to create this Dictionary: Dictionary

Solution 1:

Converting the datatype of 'Key' ("Date" in my case) element to String fixed the issue. Initially the "Date" was of DateTime DataType.

Hence when I was returning my above mentioned dictionary as a JSONResult from controller the JS method throwed an error as it was unable to stringify or serialize this dictionary.

So it's advisable to change your Dictionary's 'Key' element's datatype to String or set them as Objects.

Here's the error message I was able to debug out (providing this error message as it might help other's who may face such similar issues) :

Unhandled Exception: System.ArgumentException: Type 'System.Collections.Generic. Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[ChatData, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.

Post a Comment for "Returning Dictionary As JSON And Successfully Accessing It's Contents Withing JS Function - C#"