Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary is not supported for serialization/deserialization of a dictionary, keys must be strings or objects

I have this method that is returning Dictionary as a JsonResult when I try to deserialize this Dictionary in Ajax I am getting this error:

This is my method in MVC:

[HttpPost]
public JsonResult GetCalculateAmortizationSchedule()
{
      var data =.....
      var httpClient = new HttpClient();
      var response = httpClient.PostAsJsonAsync("http://localhost:62815/v1/APR/CalculateAmortizationSchedule", data).Result;
      var returnValue = response.Content.ReadAsAsync<Dictionary<int, AmItem>>().Result;
      return Json(returnValue);
}

This is error:

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

like image 831
nik Avatar asked May 23 '17 16:05

nik


1 Answers

This is your dictionary: Dictionary<int, AmItem>

This is what your dictionary should be: Dictionary<string, AmItem>

like image 83
Parmdeep Chhabra Avatar answered Oct 19 '22 08:10

Parmdeep Chhabra