2011-08-09 8 views
1

私は自分のコードにjQueryのgetJSONを使って呼び出す関数を持っています。私が何をすべきかわからないんだけど、getJSONを使って配列を表示

$.getJSON('/MyController/GetItems/', function (data) { 
    // somehow iterate through items, displaying ItemID, Name, and Description 
}); 

しかし、私はここにこだわっている:ここでは、関数の:

public JsonResult GetItems() 
{ 
    var items = (from x in GetAllItems() 
       select new { x.ItemID, x.ItemType.Name, x.Description }) 
       .ToList(); 


    JsonResult result = Json(items, JsonRequestBehavior.AllowGet); 

    return Json(result, JsonRequestBehavior.AllowGet); 
} 

そして、私はjQueryを使ってそれを呼んでいる場所です。私は各項目を繰り返し、ItemID、Name、およびDescriptionをアラートボックスに表示したいと思います。しかし、私が見つけたすべての例では、ちょうどのみキーを持っているか反復して表示項目を示しているが、私の項目は、表示する2つの特性よりも動きがあります。

答えて

3

これを試してみてください:

$.getJSON('/MyController/GetItems/', function (data) { 
    var name, itemId, description; 
    $.each(data, function(){ 
     name = this.Name; 
     itemId = this.ItemID; 
     description = this.Description ; 
     //You can use these variables and display them as per the need. 
    }); 
}); 
1

使用firebugあなたのjavascriptのコードをデバッグします。そこにコード内にブレークポイントを設定すると、データオブジェクトにどのような結果があるかを見ることができます。私の推測では、次のようなものを書くべきです:

$.getJSON('/MyController/GetItems/', function (data) { 
    for(var i = 0; i < data.length; i++) { // add a breakpoint here 
              // to see if reach the line and 
              // the content of data 
     // do your funky stuff here 
    } 
}); 
関連する問題