【问题标题】:How to Return JSON values from an ASP.NET page如何从 ASP.NET 页面返回 JSON 值
【发布时间】:2018-12-24 05:20:32
【问题描述】:

我正在使用 asp.net ajax JSON 创建简单的 crud 系统,我创建了函数 get all 以从 all_data.aspx 页面检索值作为 JSON 类型。但我无法检索数据。到目前为止,我尝试了以下内容

表格设计

 <table id="tbl-category" class="table table-responsive table-bordered" cellspacing="0" width="100%">              
                        <thead>
                        <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                        </tr>
                    </table>

ajex 函数

function get_all() {
                    $('#tbl-category').dataTable().fnDestroy();
                    $.ajax({
                        url: 'all_data.aspx',
                        type: "GET",
                        dataType: "JSON",
                        success: function (data) {
                            $('#tbl-category').dataTable({
                                "aaData": data,
                                "scrollX": true,
                                "aoColumns": [
                                    { "sTitle": "fname", "mData": "fname" },
                                   { "sTitle": "age", "mData": "age" },

                                    {
                                        "sTitle": "Edit",
                                        "mData": "id",
                                        "render": function (mData, type, row, meta) {
                                            return '<button class="btn btn-xs btn-success" onclick="get_category_details(' + mData + ')">Edit</button>';
                                        }
                                    },
                                    {
                                        "sTitle": "Delete",
                                        "mData": "id",
                                        "render": function (mData, type, row, meta) {
                                            return '<button class="btn btn-xs btn-primary" onclick="RemoveCategory(' + mData + ')">Delete</button>';

                                        }
                                    }

                                ]

                            });

                    },

记录表只包含名字,年龄列在这里如何将此列设置为 JSON 类型我不知道这样做请一些帮助我做到这一点我附在下面我累了到目前为止

**all_data.aspx*

    string sql = "select * from records";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter();
     da.Fill(dt);

    string sql = "{\"fname\":\"fname\",\"age\":\"age\"}";
    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(json);
    Response.End();

【问题讨论】:

  • 用这些属性创建一个类并从上述方法返回
  • 你能帮我写代码吗,先生,它更有帮助
  • 这里有一个例子,试着告诉我! ->stackoverflow.com/questions/14623327/…
  • 好的,谢谢先生,你能写出对我有帮助的代码吗?谢谢
  • 公共静态字符串 doSomething() { string sql = "select * from records"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); cmd.ExecuteNonQuery();数据表 dt = 新数据表(); SqlDataAdapter da = new SqlDataAdapter(); da.填充(dt);字符串 sql = "{\"fname\":\"fname\",\"age\":\"age\"}";响应。清除(); Response.ContentType = "应用程序/json; charset=utf-8";返回“你好”; } 这是正确的先生

标签: asp.net ajax


【解决方案1】:

安装using Newtonsoft.Json;。如何安装Newtonsoft.Json

创建一个类:

public class Employee
{
  public string fname {get; set;}
  public int age {get; set;}
}

在方法中:

public string GetEmployees()
{
   string sql = "select * from records";
   SqlCommand cmd = new SqlCommand(sql, con);
   con.Open();
   cmd.ExecuteNonQuery();
   DataTable dt = new DataTable();
   SqlDataAdapter da = new SqlDataAdapter();
   da.Fill(dt);
   List<Employee> employees = new List<Employee>();

   employees = dt.AsEnumerable()
           .Select(x => new Employee()
            {
              fname = x.Field<string>("fname"),
              age = x.Field<int>("age"),
            }).ToList();

   return JsonConvert.SerializeObject(employees);
}

如果您从 C# 方法获取正确的数据,则附加数据,例如:

$.ajax({
 type: "GET",
 url: "https://jsonplaceholder.typicode.com/todos/1",
 success: function(res) {
  $.each(res, function(i, data) {
   $("table.table").append("<tr><td>" + res.userId + "</td><td>" + res.title + "</td></tr>");
  })
 },
 error: function(xhr, status, errorThrown) {
  alert("An error occered, " + errorThrown);
 }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl-category" class="table table-responsive table-bordered" cellspacing="0" width="100%">
   <thead>
      <tr>
         <th>Id</th>
         <th>Name</th>
      </tr>
</table>

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 我应该把项目发给你吗先生,你能告诉你修复的电子邮件 ID 并发回吗
猜你喜欢
  • 2011-01-26
  • 1970-01-01
  • 2018-10-11
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
相关资源
最近更新 更多