为什么我的数据没有在mvc4中更新?

时间:2014-02-13 15:02:37

标签: c# javascript jquery ajax asp.net-mvc-4

当我要更新数据时,它会提醒我一个错误,例如“状态=问题:无法更新客户信息”。我没有db.Update(ci)运行此代码;代码,即使没有任何更新代码,它也会显示“已成功更新”。但是当我使用更新方法时,它没有执行。哪里是我无法定义的问题....这是我的控制器代码..

public ActionResult Update(ClientInfo client, string id)
        {
            //Response.Write("Id : " + id + "<br>");
            //Response.Write("Country : " + client.Country + "<br>");
            try
            {
                //if (ModelState.IsValid)
                //{

                    ClientInfo ci = db.Single<ClientInfo>("Where CId=" + id);
                    if (ci != null)
                    {
                        ci.CName = client.CName.ToString();
                        ci.CCName = client.CCName.ToString();
                        ci.Address = client.Address.ToString();
                        ci.PhoneNo = Convert.ToInt32(client.PhoneNo.ToString());
                        ci.Fax = client.Fax.ToString();
                        ci.Email = client.Email.ToString();
                        ci.Country = client.Country.ToString();
                        ci.PostalCode = Convert.ToInt32(client.PostalCode.ToString());
                        //ci.Update();
                        db.Update(ci);
                        return Json(new { msg = "Successfully Updated."});
                    }
                    else
                        return Json(new { msg = "Fail to Update Client Info." });
                //}

                //return RedirectToAction("Index");
            }
            catch
            {
                return Json(new { msg = "Problem : Fail to Update Client Info." });
            }
        }

我的脚本将数据发布到服务器

$('#btnUpdate').click(function () {
            var CId = $("#CId").val();
            var CName = $("#CName").val();
            var CCName = $("#CCName").val();
            var PhoneNo = $("#PhoneNo").val();
            var Fax = $("#Fax").val();
            var Email = $("#Email").val();
            var Address = $("#Address").val();
            var PostalCode = $("#PostalCode").val();
            var Country = $("#Country").val();

            var client1 = {
                "CId": CId,
                "CName": CName,
                "CCName": CCName,
                "PhoneNo": PhoneNo,
                "Fax": Fax,
                "Email": Email,
                "Address": Address,
                "PostalCode": PostalCode,
                "Country": Country
            };

            var lk = "/Clients/Update/" + CId;
            //alert("Test : Update " + lk + "\n" + client1.Country);
            client = JSON.stringify(client1);

            $.ajax({
                url: lk,
                type: 'POST',
                data: client,
                dataType: "json",
                success: function (data) {
                    alert("Status = " + data.msg);
                },
                error: function (data) {
                    alert("Error = " + data.msg);
                }
            });

1 个答案:

答案 0 :(得分:1)

您没有正确传递数据。您的链接也生成错误。由于您要将两个对象传递给视图,因此最好在ajax data对象中指定两个对象:

var lk = "/Clients/Update/"; // => removed the CId
//alert("Test : Update " + lk + "\n" + client1.Country);
client = JSON.stringify(client1);

$.ajax({
      url: lk,
      type: 'POST',
      data: { client: client, id = CId } // => added an object containing all the expected parameters
      dataType: "json",
      success: function (data) {
          alert("Status = " + data.msg);
      },
      error: function (data) {
          alert("Error = " + data.msg);
      }
});