记录未更新

时间:2018-12-28 05:29:18

标签: asp.net json ajax

我通过Ajax将数据和ID一起发送到update.aspx页面。我不知道这是否正确。

我检查了数据,但未将其发送到update.aspx页。该表单仅包含clear()个字段。这是当用户单击编辑按钮时,我将数据发送到update.aspx进行更新的方法:

fname,age

屏幕截图图像。

enter image description here

Ajax完整代码

data: {fname: '" + $('#fname').val() + "',age: '" + $('#age').val() + "'}" + "&id=" + id,  

update.aspx页面

$.ajax({
    type: 'POST';,
    url: 'update.aspx/doSomething',
    dataType: 'JSON',
    contentType: "application/json; charset=utf-8",
    data: { fname: '" + $('#fname').val() + "', age: '" + $('#age').val() + "'}" + "& id=" + id, 
                    success: function(data) {
    }

当我单击“编辑”按钮时,它会转到功能get_category_details以及记录ID 修改按钮代码

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

[WebMethod]
public static string doSomething(string fname, int age, int id)
{
    SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
    string sql = "update record set name ='" + fname + "', age ='" + age + "' where id = '" + id + "')";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    return "Sucess";
}

get_category_details 在文本框中显示的所有数据。

"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变量 id = data.d [0] .id

2 个答案:

答案 0 :(得分:0)

如下更改您的ajax代码

 var dataValue = { "fname": $('#fname').val(), "age": $('#age').val(), "id": id};

         $.ajax({                  
                            type:'POST';,
                            url: 'update.aspx/doSomething',
                            dataType: 'JSON',
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(dataValue),
                            success: function (data) {

                            }
              });

如下更改您的网络方法代码

public class UserClass
{
    public string fname { get; set; }
    public int age { get; set; }
    public int? id { get; set; }
}

[WebMethod]
public static string doSomething(UserClass userClass)
{
    SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
    string sql = "update record set name ='" + userClass.fname + "', age ='" + userClass.age + "' where id = '" + userClass.id + "')";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    return "Sucess";
}

答案 1 :(得分:0)

您只能像这样更新ajx函数

     $.ajax({
        type: 'POST',
        url: 'update.aspx/doSomething',
        dataType: 'JSON',
        contentType: "application/json; charset=utf-8",
        data: {
               fname:$('#fname').val(),
               age:$('#age').val(),
               id:id //this id is your like fname or age that.You have get id from your request
      } 
      success: function(data) {
     }