2012-01-31 45 views
2

我真的找不到任何示例如何將List<object>轉換爲Json格式並在C#中填充<tbody>如何將列表<object>轉換爲Json格式並在C#中填充<tbody>?

List result = new List();

MyClass m1 = new MyClass(); 
MyClass m2 = new MyClass(); 

result.Add(m1); 
result.Add(m2); 

    return Json(???); 


$.post("/Tradeshow/AddTradeShowDetail", { 
     startdate: $('#cstartdate').val(), 
     enddate: $('#cenddate').val(), 
     location: $('#clocation').val(), 
     speakerid: $('#TradeshowSpeakers').val(), 
     isnonspeaker: value 
    }, 
       function (data) { 

        if (data.length > 0) { 

          // Populate <tbody> ??? 

         } 
       }); 
+0

什麼是MyClass?它是可序列化的嗎?另外,假設MVC給出'return Json()'是否安全? – 2012-01-31 02:05:50

+1

http://stackoverflow.com/questions/7087312/how-to-convert-a-listt-to-specific-json-format檢查此 – 2012-01-31 02:06:24

+1

你試過嗎? http://stackoverflow.com/a/6366183/639960 – Akhil 2012-01-31 02:06:44

回答

1

行動

var result = new List<MyClass>(); 
MyClass m1 = new MyClass(); 
MyClass m2 = new MyClass(); 

result.Add(m1); 
result.Add(m2); 

return Json(result); 

JS

$.post("/Tradeshow/AddTradeShowDetail", { 
    startdate: $('#cstartdate').val(), 
    enddate: $('#cenddate').val(), 
    location: $('#clocation').val(), 
    speakerid: $('#TradeshowSpeakers').val(), 
    isnonspeaker: value 
}, function(data) { 

    if (data && data.d && data.d.length > 0) { 
     var rows = $.map(data.d,function(item) { 
      return ('<tr><td>' + item.Name + '</td></tr>'); 
     }).join(''); 
     $('tbody').html(rows); 
    } 
}); 

檢查清單是data.d當它回來。如果沒有,只需處理data

相關問題