MNAH
MNAH

Reputation: 71

Why my data is not updating in mvc4?

When i am going to update data it alert me a error like "Status = Problem : Fail to Update Client Info". I have run this code without db.Update(ci); code, even without any update code it shows me "Successfully Updated.". But when i am used update method it is not executing. where is the problem i can not defined.... Here is my controller code..

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." });
            }
        }

And my script for post data to the server

$('#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);
                }
            });

Upvotes: 0

Views: 57

Answers (1)

Andrei V
Andrei V

Reputation: 7508

You are not passing your data correctly. Your link is also incorrectly generated. Since you are passing two objects to your view, it's better to specify both in the ajax data object:

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

Upvotes: 1

Related Questions