Disable client-side validation in MVC 3 “cancel” submit button

微笑、不失礼 提交于 2019-11-27 11:38:39

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" />
David

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>

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;
        });
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!