2012-05-09 11 views
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の場合、キーと値のペアを返すにはどうすればよいですか?私自身のラッパークラスを作成する必要がありますか?

答えて

0

あなたは$.eachに辞書を渡し、その後indexキーとentryある値、すなわち

$.each(dict, function (key, value) { 
    var opt = $('<option>', { value:key, text: value }); 
    mySelect.append(opt); 
}); 
です
関連する問題