2012-01-05 32 views
0

我使用一個jQuery調用集合 想這是我收集如何解析jQuery中的System.Collections.Generic.List?

 PageBL pageBL = new PageBL(); 
     List<Page> pageList = pageBL.GetCategoryPageList(categoryID); 
     return pageList; 

我得到這個列表中的jQuery,

$.get("/Home/GetActionMethod/" + id, { CategoryID: id }, function (data) { 

      }); 

現在任何一個可以告訴我,這個數據是如何解析這樣我才能得到所需的結果。

+0

每個Page對象將在一個JavaScript數組。 – 2012-01-05 10:35:37

+3

我假設這是'controller'代碼...什麼是View代碼? – 2012-01-05 10:35:38

+0

@dan_l確定它將被視爲javascript數組,謝謝。 – user1006544 2012-01-05 10:36:50

回答

0

你應該用你的控制器動作return Json(pageList);

格式將取決於Page佈局。但這樣的:

[ 
    {"PageName": "Title", "SomeOtherProp": "Value"}, 
    {"PageName": "Some other page", "SomeOtherProp": "Value2"} 
] 

PageNameSomeOtherProp對應於Page

性能要遍歷的信息,處理它,你可以這樣做:

$.get("/Home/GetCategoryPageList/" + id, { CategoryID: id }, function (data) { 
    $.each(data, function(item) { 
     alert('Property from an item: ' + item.PageName); 
    }); 
}); 
+0

沒有其他的我sendign整個收集我的數據庫表數據在該集合,這就是爲什麼我不使用JSON – user1006544 2012-01-05 10:43:32

+1

祝你好運。 – jgauffin 2012-01-05 10:45:02

0

請閱讀我的話題已經打開,它可能會幫助你 Return a JSon array to $.ajax from ActionResult type method in MVC 3

var data=null; 
$.ajax({ 
     url: '/Home/GetCategoryPageList/', 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     data: {CategoryID: id} 
     success: function (msg) { 
      data = msg; 
     }, 
     complete: function() { 
      //do something with data 
      $.each(data, function(index, value) 
      { 
      $("#div1").append(value.PageName); //or other property from generic list 
      } 
     } 
     }); 

我假設你在你的控制器有一個通用的清單包含jgauffin結構(頁面名稱,標題,SomeOtherProp等)

PageBL pageBL = new PageBL(); 
     List<Page> pageList = pageBL.GetCategoryPageList(categoryID); 
     return this.Json(new { 
     msg = pageList 
     }); 
+0

$ .get($ .ajax使用的)會自動檢測json的類型,不需要指定它。 – jgauffin 2012-01-05 10:52:24