asp.net-mvc 在 MVC 3“取消”提交按钮中禁用客户端验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5363624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:00:56  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-3validationclient-side

提问by Glenn Doten

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:

好的,已经尝试了几个小时,可以使用一些帮助。我正在尝试在 MVC 3 中实现一个具有“后退”和“下一个”按钮的页面。单击后退按钮时,我想禁用客户端 MVC 验证运行,以便我的操作方法将运行并将用户发送到上一个逻辑网页。我试过这个:

<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 disableValidationonly works in MVC 2 perhaps, and I'm supposed to be doing something else in MVC 3, but cannot seem to find any examples.

但无论如何,客户端验证 JavaScript 会启动并且不会让按钮进行回发。我认为这可能disableValidation只适用于 MVC 2,我应该在 MVC 3 中做其他事情,但似乎找不到任何示例。

回答by Glenn Doten

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:

看起来在 MVC 3 中,您通过向按钮添加“取消”类来禁用按钮上的客户端验证。所以在我的例子中:

<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:

效果很好。也不需要 ID 属性。如果按钮上有实际的样式类,请执行以下操作:

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

回答by David

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

验证脚本似乎链接到提交类型input。通过更改cancel为按钮,将跳过验证:

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

回答by da_jokker

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"

对于 MVC 4(和一个 BUTTON),似乎(至少对我而言)答案只是添加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>

回答by user3963360

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;
        });
    }
});