简体   繁体   中英

json function not call via using jquery ajax mvc

I am using model pop-up and I want to call controller but json function not calling. when I use a breakpoint in jquery data fill in the textbox but function not call. kindly tell me where I am wrong.

first, I declare a variable then store password textbox value then pass password parameter then I click save and using breakpoint message show undefine and I remove previous code then I use this code its not calling function.

Javascript

<script>
function mSavePassword() {
    $.ajax({
        url: "@Url.Action("ChangePassword")",
        type: "GET",
        contentType: "application/json;charset=UTF-8",
        data: {
            Password: $('#txtcurrentpassword').val(),
            NewPassword: $('#txtpassword').val(),
            ConfirmPassword: $('#txtConformPassword').val()
        },
        dataType: "json",
        success: function (Record) {
            alert("Record  Inserted Successfully");
        },
    });
}
</script>

JSON FUNCTION

 public JsonResult ChangePassword(User U)
        {
            try
            {
                con = new SqlConnection(constring);
                con.Open();
                cmd = new SqlCommand("select User_password from BriskSecurity.dbo.Users where User_Id=" + Session["AgentID"] + "", con);
                string mPwd = Convert.ToString(cmd.ExecuteScalar());

                if (Cryptographer.Encrypt(U.Password.Trim()) != mPwd.Trim())
                {
                    TempData["old"] = "Incorrect Password";
                    return Json(TempData["old"], JsonRequestBehavior.AllowGet);
                }

                if (U.NewPassword.Trim() != U.ConfirmPassword.Trim())
                {
                    TempData["Wrong"] = "Your New Password and Confirm Password do not match";
                    return Json(TempData["Wrong"], JsonRequestBehavior.AllowGet);
                }
                U.ConfirmPassword = Cryptographer.Encrypt(U.ConfirmPassword);
                cmd = new SqlCommand("update BriskSecurity.dbo.Users set User_password='" + U.ConfirmPassword + "' where User_ID=" + Session["AgentID"] + "", con);
                cmd.ExecuteNonQuery();
                con.Close();

                TempData["PSuccess"] = "Your password has been changed successfully";
            }
            catch (Exception)
            {
                TempData["Error"] = "Password not changed due to an error Try Again";
                return Json(TempData["Error"], JsonRequestBehavior.AllowGet);
                throw;
            }

            return Json("", JsonRequestBehavior.AllowGet);

        }

I would suggest way to send dynamic url to ajax javscript from C# MVC page.

I very easy to use a form like:

<form method="post" action="@Url.Action("ChangePassword")">
   <input ... />
   <input ..../>

   <input type="submit" value="Change password" />
 </form>

and with jQuery:

$("form").on("submit", function(e) {
   mSavePassword($(this));
});


function mSavePassword($form) {
    $.ajax({
        url: $form.prop("action"),
        type: $form.prop("method"),
        data: {
            Password: $('#txtcurrentpassword').val(),
            NewPassword: $('#txtpassword').val(),
            ConfirmPassword: $('#txtConformPassword').val()
        },
        dataType: "json",
        success: function (record) {
            alert("Record  Inserted Successfully");
        }
    });
}

and do changes on server side:

[HttpPost]
public JsonResult ChangePassword(User U)

otherwise, won't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM