1

I am developing an ASP MVC web application using stored procedures via SQL Server.

I am trying to establish a confirmation's alert (Sweet Alert) in order to confirm the update of the data for a logged user.

The edit's code work perfectly without the confirmation. But once adding the confirmation alert, I face a problem with passing the parameters of the user (client) from the view to the controller via javascript function.

I tried to pass the hole model as parameter inside the onClick() but I got a JavaScript error of :

Uncaught ReferenceError: Delivery is not defined at HTMLInputElement.onclick

This is a portion of my view code (Edit page):

     <input type="button" value="Save" class="btn btn-default" onclick="Update(@Model.ClientId)">

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <script type="text/javascript">
        function Update(clientId) {
            swal({
                title: "Are you sure?",
                text: "Once modified, you will not be able to recover your profile!",
                icon: "warning",
                buttons: true,
                dangerMode: true,
            })
        .then((willDelete) => {
            if (willDelete) {
                $.ajax({
                    type: "POST",
                    data: {
                        id: clientId
                    },
                    url: "/Client/Edit",
                    dataType: "json",
                    success: function (response) {
                        swal("Poof! Your profile has been modified!", {
                            icon: "success",
                        }).then(function () {
                            window.location.href = "/Home/Index"
                        });

                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });

            }
            else {
                swal("Your profil is safe!");
            }
        });
        }
    </script>

The Controller (ClientController):

 [HttpPost]
    public JsonResult Edit(int id, Client cmodel)
    {
        try
        {

                ClientManagement cdb = new ClientManagement();
            if (cdb.UpdateDetails(cmodel))
            {
                TempData["Message"] = "Client Details Edited Successfully";
            }

            return Json("edited");
        }
        catch
        {
           return Json("Error");
        }
    }

The ClientManagement class :

  public bool UpdateDetails(Client cmodel)
    {
        try
        {
            connection();
            SqlCommand cmd = new SqlCommand("UpdateClientsInfo", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@CltId", cmodel.ClientId);
            cmd.Parameters.AddWithValue("@FirstName", cmodel.FirstName);
            cmd.Parameters.AddWithValue("@LastName", cmodel.LastName);
            cmd.Parameters.AddWithValue("@Email", cmodel.Email);
            cmd.Parameters.AddWithValue("@Phone", cmodel.Phone);
            cmd.Parameters.AddWithValue("@Address", cmodel.Address);
            cmd.Parameters.AddWithValue("@Password", cmodel.Password);

            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();

            if (i >= 1)
                return true;
            else
                return false;
        }

        catch (SqlException sqlexc)
        {
            foreach (SqlError error in sqlexc.Errors)
            {
                string msg = string.Format("{0}: {1}", error.Number, error.Message);
            }
            return false;
        }
    }

While debugging, all values of the Client (ClientId, LastName...) are Null.

Exact
  • 269
  • 6
  • 18
  • 1
    Your current code is sending a js object with only one property(`id`). If you want `LastName`, include that in the js object. If you have a form, you can send the serialized version of the form instead of adding individual properties to the js object – Shyju Jan 09 '18 at 11:02
  • @Shyju yes thats what I mean, how can I send the hole object ? and I have a form, how can send its serialized version ? – Exact Jan 09 '18 at 11:47
  • 1
    you can use the jQuery serialize method. Refer this for sample https://stackoverflow.com/questions/10803292/how-to-send-data-in-jquery-post-to-mvc-controller-which-use-viewmodel-as-paramet/10803473#10803473 – Shyju Jan 09 '18 at 22:28

1 Answers1

0

You must need to pass parameter in '(single quote).

<input type="button" value="Save" class="btn btn-default" onclick="Update('@Model.ClientId')">

Mehul Bhalala
  • 780
  • 7
  • 16
  • thx for your answer but it is not a problem of quote, the same cofirmation alert with the same code works perfectly in the example of Delete, but hier no because edit take as parameter ClientId and cmodel, and delete take only clientId – Exact Jan 09 '18 at 10:13