c# - 将数据从 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 () {
}
});
});
提前致谢 !
解决方案
尝试关注
创建一个用于向前端发送数据的类
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;
});
}
}
推荐阅读
- php - PHP 语法 ?>
我无法弄清楚 ?> <? 此代码中正在执行语法。我已经搜索了 php 语法页面,这不是一个开始或结束标记,也不是一个简短的打开标记。没有它,代码将无法编译,并且我在后面的大括号中得到了一种奇怪的颜色。
if(isset($_POST)){?> <form method="POST" action="Form_Index.php">
- android - 在同步函数中等待异步结果
- sql-server - Get-SqlErrorLog 电子邮件备份失败
- html - 如何从 html 文档中批量删除此处的整个文本
- flutter - 当您返回更改第一个下拉菜单时,取决于 Dropdown 失败断言错误
- python - 为什么我实现的mergeSort有O(n^2)?
- flutter - 如何在 Flutter 中使用 ListTile 显示列内的项目列表
- gnuradio - gnuradio 标记流到 PDU 块中缺少长度标记
- typescript - Jest 和 Typescript:覆盖默认模拟实现的推荐方法
- ios - Swift:枚举 Int,可编码用于各种情况