0
看起來這應該很容易,但我似乎無法弄清楚如何訪問我的鍵/值對:如何處理客戶端的字典?
在服務器端:
public JsonResult GetDict(int type)
{
Repository repo = new Repository();
Dictionary<int,string> dict = repo.getDict(type);
return Json(dict, JsonRequestBehavior.AllowGet);
}
一個客戶端:
var mySelect = $('#mySelect');
mySelect .empty();
mySelect .append($('<option/>', { value: "", text: "-- pick one --" }));
$.getJSON("/controller/GetDict/", { type: 1 }, function (dict, status) {
// this doesn't work
$.each(dict, function (index, entry) {
mySelect .append($('<option/>', {
value: entry.Key,
text: entry.Value
}));
});
});
如何從我的getJSON調用返回的字典中創建我的選擇列表?
事實證明我的getJSON調用拋出一個錯誤:
類型「System.Collections.Generic.Dictionary」的字典的序列化/反序列化不支持,鑰匙必須是字符串或對象。
如何返回鍵/值對,其中的鍵是int?我是否需要創建自己的包裝類?