1

I have an MVC2 C# .Net Web App. We are using the built in MVC3 Validation using the Domain class properties [Required(ErrorMessage = "Start From is required.")] and in the HTML @Html.ValidationMessageFor(model => model.StartFrom)

However, when we submit the page using the Cancel button, the validation is fired stating the "Start From is Required" and therefore not exiting the page. How can I disable the Validation on the Cancel button? Or submit the page without firing the Validation?

jrummell
  • 42,637
  • 17
  • 112
  • 171
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
  • 2
    Why are you POSTing when the user clicks Cancel? Perhaps you could change the button to a link? – jrummell Apr 01 '13 at 13:16
  • @jrummell....I found the answer I needed. See my answer below. THx – MikeTWebb Apr 01 '13 at 13:41
  • possible duplicate of [jQuery Validation plugin: disable validation for specified submit buttons](http://stackoverflow.com/questions/203844/jquery-validation-plugin-disable-validation-for-specified-submit-buttons) – jrummell Apr 01 '13 at 13:59
  • @jrummell...agreed, that's the post I got my answer from – MikeTWebb Apr 01 '13 at 14:21

2 Answers2

1

I think you need to override the default behaviour of the submit button i.e., Cancel button in your case.

Say you have the cancel button like this:

<input type="submit" id="btnCancel" value="cancel"/>

now write the jQuery to override the default behaviour

$(function(){
    $('#btnCancel').click(function(e){
        e.preventDefault();
        //or you can return false from this method.
        //return false;
    });
});
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
  • @Karthik...that's close. It desables the Validation, but the page doesn't submit. When I add $('form').submit(); the page submits but also runs the validation. – MikeTWebb Apr 01 '13 at 13:33
0

I found an answer here, on Stackoverflow :) jQuery disable validation Each of the first two answers in that link worked for me. @Karthik, thanks for the answer. It got me on the right track

Answer 1:

<input id = "theCancel" class="cancel" type="submit" value="Cancel" />

Answer 2:

$(function () {
    $('#theCancel').click(function (e) {
        $("form").validate().cancelSubmit = true;
    });
});

I chose answer 2 and put it in our global js file. All of our Cancel buttons have an id of "theCancel"

Community
  • 1
  • 1
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132