2

I really cannot find any sample how to convert List<object> to Json format and populate <tbody> in C#?

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> ???

                      }
                 });
Terminador
  • 1,328
  • 4
  • 14
  • 23

1 Answers1

1

action

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);
    }
});

Check if the list is in data.d when it comes back. If not, just process data

Brent Anderson
  • 966
  • 4
  • 6