Home > OS >  Call method with Ajax in ASP.NET MVC problem
Call method with Ajax in ASP.NET MVC problem

Time:12-05

I'm a beginner at Ajax and Js so I want my button call my action method so this is my scripts:

<div class="buttons">
    <input type="button" class="btn btn-success" id="PassCode" value="پسورد یکبار مصرف"> 
</div>

$(document).ready(function () {
    $("#$PassCode").click(function (e) {
        var form = this;
        $.ajax({
            url: '@Url.Action("RenderOnetimePassword", "Customer")',
            type: 'POST',
            data: $(form).serialize(),
            beforeSend: function () {
                $("#Password").addClass("loading");
            },
            success: function (response) {
                if (!response.success)
                    showError(response.message, response.captcha_string);
                else if (response.url)
                    window.location.href = response.url;
                else if (response.update_section)
                    $("."   response.update_section.name).html(response.update_section.html);

            },
            complete: function () {
                $("#Password").removeClass("loading");
            },
            error: function (error) {
                alert("@T("Account.ErrorNotification")");
            }
        });
    }
});

and in customer controller I have this method:

public ActionResult RenderOnetimePassword (SMSCodeVerificationModel model)
{
     // some code here
}

I want to call my action, but when I click my button, it's not working.

CodePudding user response:

hi welcome to frontend world !

you need study more about functions in ajax and jquery so try this

      $(document).on('click', function () {
   $("#PassCode").click(function () {
      
            var form = this;
            $.ajax({
                cache: false,
                url:'@Url.Action("RenderOnetimePassword", "Customer")',
                data: $(form).serialize(),
                type: 'post',
                beforeSend: function () {
                    $("#Password").addClass("loading");
                },
                success: function (response) {
                    if (!response.success)
                        showError(response.message, response.captcha_string);
                    else if (response.url)
                        window.location.href = response.url;
                    else if (response.update_section)
                        $("."   response.update_section.name).html(response.update_section.html);

                },
                complete: function () {
                    $("#Password").removeClass("loading");
                },
                error: function (error) {
                    alert("@T("Account.ErrorNotification")");
                }
            });
     });
    });

I hope its fix your problem :)

  • Related