我知道這個問題可能已被問及數百次,並且有大量的示例和可能我被低估的示例,但我必須以任何方式詢問它,因爲我不知道在哪一方(控制器,腳本或兩者)我的代碼是不正確的,還有B/C我找不到一個關於如何將對象轉換爲二維數組的示例(這是我相信結果是):我需要將MVC控制器中的List>傳遞迴ajax腳本,然後操作數組結果,但我不知道如何強制數組讀取其元素。如何將一個對象轉換爲javascript中的二維數組
[HttpGet]
public ActionResult ListBoxCustomize(string listName)
{
var result = new List<KeyValuePair<string, string>>(); // Key , Value
// Code to add items to the list
.........
return Json(new { items = result }, JsonRequestBehavior.AllowGet);
}
JScript
$.ajax(
{
success: function (data) // THIS IS WHAT IS RETURNED -> data = Object {items: Array[5]}
{
alert(items[0][0]) // Reference Error: items is not defined
OR
alert(data[0][0]) // Uncaught TypeError: Cannot read property "0" of undefined
.... and so it goes with all other cast and combinations that I've tried.
}
您是不是要調用'data.items [0] [0]'而不是'items [0] [0]'? – creemama