2017-05-23 40 views
3

我有這個方法時返回字典作爲JsonResult當我嘗試反序列化這個詞典在阿賈克斯我收到此錯誤:字典不支持字典的序列化/反序列化,密鑰必須是字符串或對象

這是我在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); 
} 

這是錯誤:

類型「System.Collections.Generic.Dictionary`2 [System.Int32,mscorlib程序,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089],[ConnectionMappingSample.Models.A mItem,ConnectionMappingSample,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'不支持字典的序列化/反序列化,鍵必須是字符串或對象。

+1

'字典':關鍵是int類型。它告訴你「鍵必須是字符串或對象」。你有什麼問題? –

+3

你應該使你的動作'async'和'await'而不是使用'.Result'。 – SLaks

回答

5

這是你的字典:Dictionary<int, AmItem>

這是你的字典應該是什麼:Dictionary<string, AmItem>

+0

感謝它現在正在工作。 – nik