【问题标题】:Data is not Display at the textboxes using Asp.net ajax使用 Asp.net ajax 在文本框中不显示数据
【发布时间】:2019-05-24 11:03:46
【问题描述】:

当我单击编辑按钮时。可编辑的数据将被传递到相关的文本框中进行编辑。但数据未显示。到目前为止我尝试了什么,我附在下面。

Response.Write(JsonConvert.SerializeObject(employees));一个东西 非静态字段需要引用

这是一个按钮

  {
     "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>';
   }

如果我单击编辑按钮 id 将成功传递给 get_category_details 方法。然后发布到 edit_return.aspx/doSome 页面 但未检索到数据。我认为收到错误 Edit_retun.aspx。

 function get_category_details(id) {
            $.ajax({
                type: 'POST',
                url: 'edit_return.aspx/doSome',
                dataType: 'JSON',
                data: "{id: '" + id + "'}",
                contentType: "application/json; charset=utf-8",
                success: function (data) {                            

                    $("html, body").animate({ scrollTop: 0 }, "slow");
                    isNew = false
                    id = data.id     
                    $('#fname').val(data.fname);
                    $('#age').val(data.age);               

                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText);            
                }

            });
        }

Edit_retun.aspx

public class Employee
        {

            public string id { get; set; }
            public string fname { get; set; }
            public string age { get; set; }
        }

 [WebMethod]
            public static string doSome(int id)
            {
                SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
                string sql = "select * from record where id='" + id + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                List<Employee> employees = new List<Employee>();

                employees = dt.AsEnumerable()
                        .Select(x => new Employee()
                        {
                            id = x.Field<int>("id").ToString(),
                            fname = x.Field<string>("name"),
                            age = x.Field<int>("age").ToString(),
                        }).ToList();
                Response.Write(JsonConvert.SerializeObject(employees));
                return JsonConvert.SerializeObject(employees);
            }

HTML 表单

  <form  id="frmProject" runat="server">
            <div>
                <label class="form-label">First Name</label>     
               <input type="text" id="fname" name="fname" class="form-control"  required />

            </div>
            <div class="form-group" align="left">
             <label class="form-label">Age</label>
                <input type="text" id="age" name="age" class="form-control"  required />
            </div>
            <div> 
               <input type="button" id="b1" value="add" class="form-control" onclick="addProject()" />

            </div      
        </form>

【问题讨论】:

  • 将此添加到方法顶部 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  • ScriptMethod 命名空间错误先生
  • Response.Write(JsonConvert.SerializeObject(employees));响应关键字得到错误先生非静态字段所需的对象引用。如果你写它,如何以正确的方式写。对我更有帮助。
  • 为什么在返回 JSON 响应时使用 Response.Write? ScriptMethod 的命名空间是 System.Web.Script.Services。
  • 你能试试 alert (data.d);在 ajax 的成功部分。

标签: asp.net json ajax


【解决方案1】:

您不能在static 方法中使用Response.Write。您的 doSome 方法必须是 non-static 方法。或者你可以从你的方法中删除Response.write,因为我猜这里不需要它。

【讨论】:

  • 谢谢先生,我可以将数据输入到 ajex 函数中,我无法转换到相关的文本框,你可以检查上面代码中的 ajex 成功函数
【解决方案2】:
 [WebMethod]
    public List<Employee> doSome(int id)
    {
        SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
        string sql = "select * from record where id='" + id + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        con.Open();
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        List<Employee> employees = new List<Employee>();

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

Your List<Employee> will be serialize automatically no need to serialize.
In ajax success 
  $.ajax({

            success: function(data){
          data=data.d;
     // now data is JavaScript array of employee object 
},

        });

【讨论】:

  • 查看完整代码。我认为您无法从 Web 方法获得响应,我已经解决了从 Web 服务返回对象类型并在 ajax 成功中捕获它
  • console.log(data);我没有检索任何数据先生 ajax 成功
  • unknown web method dosome parameter name method name error display when I click edit button
  • 在 $.ajax() 调用中只有成功参数在我的代码中。在您的代码中,仅用我的成功代码替换成功方法,并在 webservice 文件中检查 // 要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请取消注释以下行。 [System.Web.Script.Services.ScriptService]
  • 您的服务文件应该是 edit_return.asmx webservice 文件,但您使用的是 edit_return.aspx,它在 $.ajax({ url: 'edit_return.aspx/doSome',) 中检查过一次错误
猜你喜欢
  • 1970-01-01
  • 2021-07-28
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-16
  • 1970-01-01
  • 2020-11-29
相关资源
最近更新 更多