使用json在ajax datatable中获取数据

时间:2016-11-11 11:38:02

标签: jquery json ajax

我想用我从MVC Action方法返回的Json数据填充我的HTML表格。但它不会创建任何行。

MVC Action方法的一段代码:

foreach (ProviderModel providers in allProviders)
{
   string providerName = providers.ProviderName;
   string providerCode = providers.ProviderCode;

   DGSAPILogFileService.MessageDetail messageDetail = null;
   DGSAPILogFileService.Message message = (from DGSAPILogFileService.Message msg in qualifyResponse.MessageList where msg.ProviderCode == providerCode
   select msg).FirstOrDefault();

   if (message != null)
      messageDetail = (from DGSAPILogFileService.MessageDetail md in message.MessageDetailList
     where md.MessageCode == totalProviderProcessingTimeMilliseconds
     select md).FirstOrDefault();

     if (messageDetail != null)
     {
         double processingTime = 0.0;
         double.TryParse(messageDetail.MessageText, out processingTime);
         if (processingTime > 0)
         processingTime /= 1000;

         if (!providerQualificationTimes.ContainsKey(providerName))
         {
             providerQualificationTimes.Add(providerName, Math.Round(processingTime).ToString());
         }
         else
         {
             providerQualificationTimes[providerName] = Math.Round(processingTime).ToString();
         }
      }
      else
      {
         if (!providerQualificationTimes.ContainsKey(providerName))
         {
             providerQualificationTimes.Add(providerName, "0");
         }
      }
}
return Json(providerQualificationTimes);

这是Ajax:

function GetProviderQualificationTimeData() {
    var urlGetProviderQualificationTimeData = "@Url.Action(MVC.DPN.ActionNames.GetProviderQualificationTimeData, MVC.DPN.Name)";
    var data = { pqtVM: { SessionId: sessionId } };
    $.ajax({
        data: JSON.stringify(data),
        url: urlGetProviderQualificationTimeData,
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        success: function (obj) {
        $('#tblProviders').dataTable({
        "columns": [
        { "data": "providerName" },
        { "data": "processingTime" }
        ]
        });
    }
    });
    }
}

我可以看到我在obj中以下列格式获取数据

obj = Object {AT&T: "0", Bright House: "0", Cable One: "0", CenturyLink: "0", Charter: "0"…}

我做错了什么?

0 个答案:

没有答案