【问题标题】:How to convert List<object> to Json format and populate <tbody> in C#?如何将 List<object> 转换为 Json 格式并在 C# 中填充 <tbody>?
【发布时间】:2012-01-31 02:02:45
【问题描述】:

我真的找不到任何示例如何将List&lt;object&gt; 转换为 Json 格式并在 C# 中填充&lt;tbody&gt;

列表结果 = 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> ???

                      }
                 });

【问题讨论】:

标签: c# jquery .net ajax json


【解决方案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

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2015-09-22
    • 1970-01-01
    • 2014-09-09
    • 2019-05-08
    • 2019-12-13
    • 2010-09-15
    • 2012-02-21
    • 2015-07-25
    相关资源
    最近更新 更多