Disable client-side validation in MVC 3 "cancel" submit button

asp.net Mvcasp.net Mvc-3ValidationClient Side

asp.net Mvc Problem Overview


OK, been trying things for hours and could use some help. I'm trying to implement a page in MVC 3 that has "back" and "next" buttons. When the back button is clicked I want to disable client-side MVC validation from running so that my action method will run and send the user to the previous logical web page. I've tried this:

<script type="text/javascript">
  document.getElementById("backButton").disableValidation = true;
</script>

and this:

<input type="submit" name="backButton" value="← Back" 
 title="Go back to step 1." disableValidation="true" />

But no matter what, the cilent-side validation JavaScript kicks in and won't let the button do its post-back. I'm thinking that disableValidation only works in MVC 2 perhaps, and I'm supposed to be doing something else in MVC 3, but cannot seem to find any examples.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

What is this mystical force that causes the answer to reveal itself as soon as you post a question somewhere?

It looks like in MVC 3 you disable client-side validation on a button by adding the class "cancel" to it. So in my example:

<input type="submit" name="backButton" value="← Back"
 title="Go back to step 1." class="cancel" />

works great. And no ID attribute is needed either. If you have an actual style class on the button, just do this:

<input type="submit" name="backButton" value="← Back"
 title="Go back to step 1." class="style-name cancel" />

Solution 2 - asp.net Mvc

I know... very old question.. Yet it was the top of the search results when I looked.

For MVC 4 (and a BUTTON) it seems (at least for me) that the answer is simply adding the formnovalidate="formnovalidate"

 <button type="submit" class="btn btn-outline-success border-0" formnovalidate="formnovalidate" name="command" value="Back" title="Back">
    <span class="fas fa-arrow-left" aria-hidden="true"></span>
    &nbsp;Back
</button>

Solution 3 - asp.net Mvc

The validation scripts seem to be linked to the submit type input. By changing cancel to a button, validation is skipped:

<button type="button" onclick="document.location.href('Index')">Cancel</button>

Solution 4 - asp.net Mvc

I use this for button

$("button").each(function (elem) {
    var button = $($("button")[elem]);
    button.addClass('cancel');

    if (button.attr('type') == 'submit') {

        button.click(function (e) {
            var validator = button.closest('form').validate();
            validator.cancelSubmit = true;
        });
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlenn DotenView Question on Stackoverflow
Solution 1 - asp.net MvcGlenn DotenView Answer on Stackoverflow
Solution 2 - asp.net Mvcda_jokkerView Answer on Stackoverflow
Solution 3 - asp.net MvcDavidView Answer on Stackoverflow
Solution 4 - asp.net Mvcuser3963360View Answer on Stackoverflow