C# 为什么我的数据没有在mvc4中更新?
当我要更新数据时,它会提醒我一个错误,如“状态=问题:无法更新客户端信息”。我在没有db.Update(ci)的情况下运行了此代码;代码,即使没有任何更新代码,它也会显示“已成功更新”。但当我使用更新方法时,它不会执行。我无法定义的问题在哪里。。。。这是我的控制器代码C# 为什么我的数据没有在mvc4中更新?,c#,javascript,jquery,ajax,asp.net-mvc-4,C#,Javascript,Jquery,Ajax,Asp.net Mvc 4,当我要更新数据时,它会提醒我一个错误,如“状态=问题:无法更新客户端信息”。我在没有db.Update(ci)的情况下运行了此代码;代码,即使没有任何更新代码,它也会显示“已成功更新”。但当我使用更新方法时,它不会执行。我无法定义的问题在哪里。。。。这是我的控制器代码 public ActionResult Update(ClientInfo client, string id) { //Response.Write("Id : " + id + "<
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." });
}
}
您没有正确地传递数据。您的链接也生成错误。由于您要向视图传递两个对象,因此最好在
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);
}
});
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);
}
});