user12345
user12345

Reputation: 191

How to add confirmation popup on button click in MVC

This is what I am trying to accomplish.

1) when a user click save, a pop up will come up 2) the pop up will say, do you want to perform XYZ. yes to continue, no back to the screen, and nothing 3) if no, just close the pop-up. I have jquery function to detect if cancel button is clicked, the popup will be closed. 4) if yes, call ajax to perform. After Ajax perform 2nd operation

what to do about number 4. ajax is the asynchronous function. I need to finish it to do the second function.

What is the advice? I don't want to deviate from the design too much (unless I have no choice) since there are some standard I have to follow

thanks

pseudo code

@using (Html.BeginForm("action1", "controller1", FormMethod.Post, new { id = "form1" }))
{

    @* various controls*@
    <input type="submit" name="save1" id="Save1" value="Save" onclick="DoXY(event);"  /> 
}

<script type="text/javascript">
    function DoXY(e) {
        if (DoX())
            Doy();
    }
    function DoX()
    {
                $.post('@(Url.Action("funcX", "controller1"))', postData, function (result) {
                    if (result.Result != 'True')
                    {
                       //what to do, I know return false does not work
                    }
                    else
                    {
                       //what to do, I know return true does not work

                     }

                });
                function DoY()
                {
                       //do something
                       $('#form1').submit(); //this will continue to next action
                }
    }
</script>

Upvotes: 1

Views: 799

Answers (1)

Cossintan
Cossintan

Reputation: 114

You could just call DoY() within DoX() where you need it.

If you are not allowed to do that because of your standards you could pass an anonymous callback function to DoX() like so:

function DoXY(e) {
    DoX(function (result) {
        if (result) {
            Doy();
        }
    });
}

And then within DoX() you just call that anonymous function with the result as parameter:

function DoX(callback)
{
    $.post('@(Url.Action("funcX", "controller1"))', postData, function (result) {
        callback(result.Result == 'True');
    });
}

Upvotes: 1

Related Questions