简体   繁体   English

如何将数据表从C#代码传递到jQuery

[英]How to pass a dataTable from C# code behind to jQuery

I have string to pass as a table of data from C# code behind to jQuery. 我有字符串要作为数据表从C#代码传递到jQuery。 for that I am using two functions: 为此,我正在使用两个功能:

C# C#

[System.Web.Services.WebMethod(EnableSession = true)]
public static List<ListItem> GetImageArray(string AccNo)
{
    string result = string.Empty;
    var obj = new AccountTransaction();

    DataTable dt = obj._commonobj.SearchAccNo(AccNo, "", "GETIMAGE");
    List<ListItem> datas = new List<ListItem>();

    if (dt.Rows.Count > 0)
    {
        foreach (DataRow row in dt.Rows)
        {
            string CustImg = Convert.ToString(row["Customer Image"]);
            string SignImg = Convert.ToString(row["Sign"]);

            ListItem listitem = new ListItem(CustImg, SignImg);
            datas.Add(listitem);                  
        }
    }
    return datas;
}

Client-side 客户端

$.ajax({
    type: "POST",
    url: "AccountTransaction.aspx/GetImageArray",
    data: "{'AccNo':'" + col1 + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnImgSuccess,
    failure: function (response) {
        alert(response.d);
    }
});

function OnImgSuccess(response) {
    alert(response.d);
    });
}

return datas; returns 2 row and alert(response.d) is not showing anythig 返回2行,并且alert(response.d)未显示anythig 在此处输入图片说明

I tried using $.map(data, function (listitem) function but no result . 我尝试使用$.map(data, function (listitem) function但没有结果。

 $.map(data, function (listitem) {
    $('<tr> <td>' + listitem.CustImg + '</td> <td>' + listitem.SignImg + ' </td> </tr>').appendTo(".tblData");
 });

Please help .! 请帮忙 。!

Your array is there in the d property, so you can use the map function but using the data.d instead of just data . 数组位于d属性中,因此可以使用map函数,但可以使用data.d而不是data

Then the ListItem properties are Text and Value , so your code should look like this: 然后ListItem属性是TextValue ,因此您的代码应如下所示:

$.ajax({
    type: "POST",
    url: "AccountTransaction.aspx/GetImageArray",
    data: "{'AccNo':'" + col1 + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: onImgSuccess,
    failure: function (response) {
        alert(response.d);
    }
});

function onImgSuccess(data) {
    $.map(data.d, function (listitem) {
        $('<tr> <td>' + listitem.Text + '</td> <td>' + listitem.Value + ' </td> </tr>').appendTo(".tblData");
    });
}

使用以下代码。

alert($.parseJSON(response.d));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM