【问题标题】:jquery Ajax Value Not Save correctly in MvcjQuery Ajax 值未在 Mvc 中正确保存
【发布时间】:2016-08-02 02:51:26
【问题描述】:

我正在使用 Jquery Ajax 代码

$("#savecustomer").click(function() {
  var customer = {};
  debugger;
  customer.Customerid = $("#userid").text();
  customer.PhoneNo = $("#txtphoneno").val();
  customer.Email = $("#txtemail").val();
  customer.Password = $("#txtpwd").val();
  customer.Name = $("#txtname").val();
  customer.Shiping_Address = $("#txtsa").val();
  customer.Pin = $("#txtpin").val();
  $.ajax({
    url: '/Home/AddCustomerInfo',
    method: 'Post',
    dataType: "json",
    data: '{customer: ' + JSON.stringify(customer) + '}',
    success: function() {
      alertify.log("Your Data saved Successfully...", "Success", 3000);
    },
    error: function() {
      alertify.log("Data Not Save ......", "error", 3000);
    }
  });
});

和 Mvc 控制器函数名称

public void AddCustomerInfo(Customerinformation customer)

   public void AddCustomerInfo(Customerinformation customer)
    {

        using (TestEntities db = new TestEntities())
        {
            db.spAddCustomer(customer.Customerid, customer.PhoneNo, customer.Email, customer.Password, customer.Name, customer.Shiping_Address, customer.Pin);

        }
    }

这个函数执行storepeocedure并将一些数据保存到数据库中。

当运行我的代码时,数据不会被引入数据库 int 字段插入 0 和 varchar 字段插入 null。 就这样DataBase Data

【问题讨论】:

  • 将您的data 选项更改为data: customer,,它会很好地绑定,但是您的代码表明您的视图存在许多其他问题。如果您正确生成它,那么它只是data: $('form').serialize()

标签: jquery ajax asp.net-mvc c#-4.0 entity-framework-4


【解决方案1】:

试试这个

 $.ajax({
            url: '@Url.Action("AddCustomerInfo", "Home")',
            data: JSON.stringify({ customer: customer }),
            dataType: 'json',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            success: function() {
            alertify.log("Your Data saved Successfully...", "Success", 3000);
            },
            error: function() {
            alertify.log("Data Not Save ......", "error", 3000);
            }

        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 2021-11-23
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多