Javascript 更新表的AJAX调用变为空-ASP.NET

Javascript 更新表的AJAX调用变为空-ASP.NET,javascript,c#,asp.net,ajax,Javascript,C#,Asp.net,Ajax,我试图通过以下代码使用Ajax调用更新我的表 var customer = {}; customer.CustomerId = row.find(".CustomerId").find("span").html(); customer.Nome = row.find(".Nome").find("span").html(); customer.Tipo = row.find(".Tipo").find("span").html();

我试图通过以下代码使用Ajax调用更新我的表

 var customer = {};
        customer.CustomerId = row.find(".CustomerId").find("span").html();
        customer.Nome = row.find(".Nome").find("span").html();
        customer.Tipo = row.find(".Tipo").find("span").html();
        customer.NCM = row.find(".NCM").find("span").html();
        customer.Contabil = row.find(".Contabil").find("span").html();

        $.ajax({
            type: "POST",
            url: "/Home/UpdateCustomer",
            data: '{customer:' + JSON.stringify(customer) + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    });
但是,当我单击更新该值时,它返回System.NullReferenceException,我看不出哪里做错了

这是我的控制器代码

 public ActionResult UpdateCustomer(Customer customer)
    {
        using (CustomersEntities entities = new CustomersEntities())
        {
            Customer updatedCustomer = (from c in entities.Customers
                                        where c.CustomerId == customer.CustomerId
                                        select c).FirstOrDefault();
            updatedCustomer.Nome = customer.Nome;
            updatedCustomer.Tipo = customer.Tipo;
            updatedCustomer.NCM = customer.NCM;
            updatedCustomer.Contabil = customer.Contabil;
            entities.SaveChanges();
        }

        return new EmptyResult();
    }

System.NullReferenceException显示updatedCustomer或customer的值为null。您需要检查哪个值为null

当您想要更新EF中的enity时,需要添加如下代码:

    context.Entry(existingBlog).State = EntityState.Modified;
但是,为什么您需要在更新之前查询实体?我想您应该这样做:

    public ActionResult UpdateCustomer(Customer customer)
    {
      using (CustomersEntities entities = new CustomersEntities())
       {
          entities.Entry(customer).State = EntityState.Modified;
          entities.SaveChanges();
       }
       return new EmptyResult();
    }

System.NullReferenceException显示updatedCustomer或customer的值为null。您需要检查哪个值为null

当您想要更新EF中的enity时,需要添加如下代码:

    context.Entry(existingBlog).State = EntityState.Modified;
但是,为什么您需要在更新之前查询实体?我想您应该这样做:

    public ActionResult UpdateCustomer(Customer customer)
    {
      using (CustomersEntities entities = new CustomersEntities())
       {
          entities.Entry(customer).State = EntityState.Modified;
          entities.SaveChanges();
       }
       return new EmptyResult();
    }

下面的AJAX回调实际上传递了包含JSON对象的JSON字符串,这是错误的,导致
customer
包含null值,该值引发
NullReferenceException
(强烈建议在使用EF实体上下文之前进行null检查):

不要像那样传递JSON字符串,只需使用
data:JSON.stringify(customer)
或直接使用
data:customer
传递整个对象,然后删除
contentType
设置:

$.ajax({
    type: "POST",
    url: "/Home/UpdateCustomer",
    data: customer, 
    dataType: "json",
    success: function (result) {
        // do something
    }
});
关于第二个问题,从表查询中选择的数据没有更新,这取决于EF的自动跟踪设置。如果禁用了自动跟踪,则需要在使用
SaveChanges()
之前设置
EntityState.Modified

注意事项:

1) 您可以在中看到使用EF更新现有数据的所有方法


2) 包含从表内容发出AJAX请求的示例。

下面的AJAX回调实际上传递了包含JSON对象的JSON字符串,这是错误的,导致
customer
包含null值,该值引发
NullReferenceException
(强烈建议在使用EF实体上下文之前进行null检查):

不要像那样传递JSON字符串,只需使用
data:JSON.stringify(customer)
或直接使用
data:customer
传递整个对象,然后删除
contentType
设置:

$.ajax({
    type: "POST",
    url: "/Home/UpdateCustomer",
    data: customer, 
    dataType: "json",
    success: function (result) {
        // do something
    }
});
关于第二个问题,从表查询中选择的数据没有更新,这取决于EF的自动跟踪设置。如果禁用了自动跟踪,则需要在使用
SaveChanges()
之前设置
EntityState.Modified

注意事项:

1) 您可以在中看到使用EF更新现有数据的所有方法


2) 包含从表内容发出AJAX请求的示例。

您没有发送有效的JSON<代码>数据:{customer}应该这样做。或者:
{customer:customer}
JSON.stringify({customer:customer})
(作为经验法则:永远不要手工编写JSON,永远不要使用stringify,除非你a)知道它的功能b)绝对肯定你必须使用它)@ChrisG它部分工作正常,没有更多的例外,但是现在我的数据库中的数据没有更新;jQuery显然会将数据转换为查询字符串,反之亦然。不过,您应该在服务器端记录
实体
,以缩小问题的范围。这里的罪魁祸首可能是
contentType:application/json
。我创建了一个示例来演示如何通过AJAX传递表内容并获得结果,只需使用
data:customer
,并从AJAX回调中删除
contentType
选项。您没有发送有效的JSON<代码>数据:{customer}应该这样做。或者:
{customer:customer}
JSON.stringify({customer:customer})
(作为经验法则:永远不要手工编写JSON,永远不要使用stringify,除非你a)知道它的功能b)绝对肯定你必须使用它)@ChrisG它部分工作正常,没有更多的例外,但是现在我的数据库中的数据没有更新;jQuery显然会将数据转换为查询字符串,反之亦然。不过,您应该在服务器端记录
实体
,以缩小问题的范围。这里的罪魁祸首可能是
contentType:application/json
。我创建了一个示例来演示通过AJAX传递表内容并获得结果,只需使用
data:customer
,并从AJAX回调中删除
contentType
选项。它继续给我带来NullReference异常,我注意到更新哪个字段并不重要,该异常仅发生在此行updatedCustomer.Nome=customer.Nome;它继续给我NullReference异常,我注意到不管我更新哪个字段,异常只发生在这一行updatedCustomer.Nome=customer.Nome;