2014-02-13 97 views
0

當我要更新數據時,它會提醒我一個錯誤,如「狀態=問題:無法更新客戶端信息」。我已經運行這個代碼沒有db.Update(ci);代碼,即使沒有任何更新代碼,它顯示我「成功更新。」。但是,當我使用更新方法它不執行。這裏是不能定義我的問題....這裏是我的控制器代碼..爲什麼我的數據沒有在mvc4中更新?

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

。您的鏈接也生成不正確。既然你是路過兩個對象到您的視圖,最好在ajaxdata對象同時指定:

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); 
     } 
}); 
+0

不顯示任何錯誤,但還沒有更新.. BOSS – MNAH

相關問題