13

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?

4 Answers 4

30

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have <form name="Form1" method="post" action="@Url.Action("PostForm", "Job")" id="Form1"> as my form action and none of the solutions I tried work. They all show a confirmation dialogue but then dont submit the form data if you click "yes". Any suggestions would be greatly appreciated.
I suggest to create a separate question including all the related information. If you take the effort to create a jsfiddle and include the link in your question you will get an answer in no time.
5
        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()

Comments

5

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

Comments

0

I believe this can be done by overriding the submit button using jquery. Jquery .submit()

This way, when the person hits submit you can show a message and either submit it or cancel it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.