Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Having a confirmation button with a Form

I have an strongly typed view for my model and what I'd like is that when the user clicks on submit, a confirmation box pop up confirming that the user does indeed wish to submit the form, if they click cancel then it shouldn't fire the HttpPost Action for that View, is this possible?

like image 950
CallumVass Avatar asked Apr 16 '12 09:04

CallumVass


3 Answers

Of course it is possible. I like to use an unobtrusive approach. Here is a simplified example:

jQuery(document).ready(function () {
  jQuery('[data-confirm]').click(function (e) {
    if (!confirm(jQuery(this).attr("data-confirm")))
    {
      e.preventDefault();
    }
  });
});

Then you only need to add a data-confirm attribute to your submit button for example

<input type="submit" data-confirm="are u sure?" />

Of course you can use this attribute on links, buttons, etc. you are not restricted to submit buttons only, and if you want to implement a fancier confirm dialog later than you will have to replace the code only in one place.

like image 64
Hari Avatar answered Nov 15 '22 11:11

Hari


        function doSubmit()
        {                                 
                if(window.confirm("ARE YOU SURE TO PERFORM THIS ACTION"))
                {                       
                    return true;
                }
                else return false;                
        }

call doSubmit() function on onsubmit event of the form, Eg- onsubmit="return doSubmit()

like image 31
Ravi Jain Avatar answered Nov 15 '22 12:11

Ravi Jain


you can add a simply jQuery call for that.

at the end of your view add:

<script type="text/javascript">

    $("form").submit(function() {

        return confirm('Are you sure?');

    });

</script>

or, add a

onsubmit="return confirm('Are you sure?');"

as a new element property

like image 5
balexandre Avatar answered Nov 15 '22 12:11

balexandre