1

我正在尝试使用 jquery 对话框表单将数据插入数据库。

这是我的jQuery:

    $(function () {
    $("#dialogregister").dialog({
    autoOpen: false,
    width: 800,
    height: 450,
    modal: true,
    show: 'blind',
    buttons: {
        'Register': function (event) {
            var fname = $("#txt_firstName").val();
            var midname = $("#txt_middleName").val();
            var lname = $("#txt_lastName").val();
            var gender = $('input:radio[name=gender]:checked').val();
            var address = $("#txt_Address").val();
            var ph = $("#txt_Phone").val();
            var email = $("#txt_Email").val();

            $.ajax({
                type: 'POST',
                url: '../Main.aspx/AddNewItem',
                data: '{"fname":"' + fname + '", "midname":"' + midname + '", "lname":"' + lname + '", "gender":"' + gender + '", "address":"' + address + '", "phone":"' + ph + '", "email":"' + email + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d) {
                        alert("Successfully added new item");
                    }
                },
                error: function () {
                    alert("Error! Try again...");
                }
            });
            $(this).dialog('close');
        },

        'Cancel': function () {
            $(this).dialog('close');
        }
    },
});

$("#signup").click(function (event) {
    event.preventDefault();
    $("#dialogregister").dialog("open");

});
});

当我单击“注册”按钮时应该调用它,但我收到错误消息。

     public static void AddNewItem(string fname, string midname, string lname, string gender, string address, string phone, string email)
         {
            Person person = new Person() { Fname=fname, Mname=midname, Lname=lname, Gender=gender, Address=address, Phone=phone, Email=email };

    //add your logic to insert item into database
         Utility ut = new Utility();
         SqlConnection con = ut.openConnection();
         string sqlStatement = "insert into tbl_Users values('" + fname + "','" + midname + "','" + lname + "','" + 
                                                            gender + "','" + address + "','" + phone + "','" + email + "');";
         SqlCommand cmd = new SqlCommand(sqlStatement, con);
         cmd.ExecuteNonQuery();
    }
    }

    public class Person
    {
        public string Fname{ get; set; }
        public string Mname { get; set; }
        public string Lname { get; set; } 
        public string Gender { get; set; }
        public string Address { get; set; }
        public string Phone{ get; set; }
        public string Email { get; set; }
    }

我做错了吗?如何将值从对话框传递到服务器端方法?

4

2 回答 2

0

您可以将数据作为 JSON 传递给服务器

var fname = $("#txt_firstName").val();
var midname = $("#txt_middleName").val();
var lname = $("#txt_lastName").val();
var gender = $('input:radio[name=gender]:checked').val();
var address = $("#txt_Address").val();
var ph = $("#txt_Phone").val();
var email = $("#txt_Email").val();

var postData = {
     Fname:fname,
      Mname : midname,
     Lname :lname ,
      Gender :gender ,
     Address :address ,
     Phone:ph ,
     Email :email 

};

$.ajax({
  type: 'POST',
  url: '../Main.aspx/AddNewItem',
  contentType:'application/json',
  data: JSON.stringify(postData),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (msg) {
       if (msg.d) {
                alert("Successfully added new item");
             }
   },
于 2013-05-20T14:24:47.563 回答
0

可能有很多事情,但有两件事会跳出来:

  1. 使用 [WebMethod] 属性标记您的 AddNewItem() 方法(使用 System.Web.Services)。

  2. 也许你的网址不好?尝试“Main.aspx/...”。当然,这取决于您的目录结构,但我假设您的 Web 方法代码与提供 javascript 的页面背后的代码位于同一文件中。

于 2013-09-23T16:58:20.340 回答