首页 > 解决方案 > 将数据从 WebMethod 返回到 ajax

问题描述

我需要从 c#(WebMethod) 将 DataTable 返回到 Ajax。我正在向 WebMethod 发送“值”。值被接收并用作下面代码中的过程的参数 (getObj.getValuesTableAdapter(Value);)。

然后过程返回 datatable(dtObj) 并且 ajax 应该在“成功”部分接收它。我需要这些代码的帮助。我尝试了一切,但失败了。

我不能像这样直接从 [WebMethod] 返回 DataTable。在以某种方式发送给客户端之前,我需要将 DataTable 转换为 JSON。

这是我的 C# 代码:

[WebMethod(EnableSession = true)]
public static DataTable GetObject(int Value)
{
    LogicTableAdapters.getValuesTableAdapter getObj = new LogicTableAdapters.getValuesTableAdapter();

    DataTable getObj = getObj.getValuesTableAdapter(Value);
    DataTable dtObj = new DataTable();
    dtObj.Columns.AddRange(new DataColumn[4]{ 
        new DataColumn("ObjectID", typeof(string)), 
        new DataColumn("ObjectName", typeof(string)), 
        new DataColumn("ObjectValue", typeof(string)), 
        new DataColumn("ParentID", typeof(int)),
    });

    foreach (DataRow dr in getObj.Rows)
    {
        dtCh.Rows.Add(dr["ObjectID"].ToString(), dr["ObjectName"] == DBNull.Value ? null : dr["ObjectValue"].ToString(), dr["ParentID"].ToString());
    }
    return dtObj;
}

这是我的ajax:

$(document).on('click', ".Btn", function () {
    header = $(this).closest('tr').find('.ColumnID').text()
    console.log(Value);   

    $.ajax({
        type: "POST",
        url: "MyAdmin.aspx/GetObject",
        data: JSON.stringify({ 'Value': Value }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function () {
           //code that should receive datatable to be displayed
        },

        error: function () {
        }
    });
});

提前致谢 !

标签: c#jqueryajaxwebforms

解决方案


尝试关注

创建一个用于向前端发送数据的类

 public class DataForClientSide
    {
        public string id { get; set; }
        public string name { get; set; }
        public string value{ get; set; }
    }

编辑您的网络方法如下

[WebMethod(EnableSession = true)]

    public static DataForClientSide[] GetObject(int Value)
    {
List<DataForClientSide> details = new List<DataForClientSide>();
        LogicTableAdapters.getValuesTableAdapter getObj = new LogicTableAdapters.getValuesTableAdapter();

        DataTable getObj = getObj.getValuesTableAdapter(Value);
        DataTable dtObj = new DataTable();
        dtObj.Columns.AddRange(new DataColumn[4]{ new DataColumn("ObjectID", typeof(string)), new DataColumn("ObjectName", typeof(string)), new DataColumn("ObjectValue", typeof(string)), new DataColumn("ParentID", typeof(int)),       

                    });

        foreach (DataRow dr in getObj.Rows)
        {
                        DataForClientSide Info= new DataForClientSide();
                        Info.id = dr["ObjectID"].ToString();
                        Info.name = dr["ObjectName"].ToString();
                        Info.value = dr["ObjectValue"].ToString();
                        //multiple data as u want. . . . . 
                        details.Add(Info);

        }
        return details.ToArray();
    }

在您的 ajax 函数中编写以下代码以获取值

 success: function (data) {
               if(data.d.length>0)
               {
                 $.each(data,function(i,values){
                 //you can get all values as per each iteration as follow
                 //to get id
                 values.id;
                 //to get name
                 values.name;
                 //to get value
                 values.value;
                 });
               }

            }

推荐阅读