是否有可能“入侵" ASP.NET MVC 3中支持的有效验证? [英] Is it possible to "hack into" the unobtrusive validaton supported in ASP.NET MVC 3?

查看:110
本文介绍了是否有可能“入侵" ASP.NET MVC 3中支持的有效验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用基于数据注释的内置客户端非侵入式验证,但是在知道通过之后,我自己进行ajax表单提交.

类似于jQuery的位

$('#form').submit(function(e) {
  e.preventDefault();

  if (PassesUnobtrusiveValidation()) {
    // ajax submit form
  }
});

是否存在一个PassedValidation事件或类似的事件(内置于默认的基于ASP.NET MVC 3数据注释的客户端验证中)?我可以加入(例如示例中的psuedocode)吗? >

我想这样做,因此我仍然可以利用基于数据注释的验证,然后按照自己的意愿异步提交表单.我希望避免在客户端中编写通用验证,让asp.net mvc 3替我处理它,然后使用$.ajax();

提交所需的表单.

解决方案

如果使用的是jquery.validate:

$('#myForm').submit(function() {
    if ($(this).valid()) {
        // Client side validation passed => submit the form
    }
});

另一种可能性是钩上插件选项:

$.validator.setDefaults({
    submitHandler: function(form) {
        // Client side validation passed => submit the form
    }
});

如果您正在使用MSAjax,那么祝您好运.

I want to be able to use the built-in, data-annotation based, client-side unobtrusive validation, but do my own ajax form submission after I know it passes.

Something like this jQuery bit:

$('#form').submit(function(e) {
  e.preventDefault();

  if (PassesUnobtrusiveValidation()) {
    // ajax submit form
  }
});

Is there a PassedValidation event, or something like it, (built into the default, asp.net mvc 3 data-annotation based client side validation) I can hook into (like the psuedocode in the example)?

I want to do this so I can still take advantage of the data-annotation based validation, and then submit the form asynchronously how I want to. I wish to avoid writing common validation in the client, letting asp.net mvc 3 take care of it for me, and then submitting the form how I want to, using $.ajax();

解决方案

If you are using jquery.validate:

$('#myForm').submit(function() {
    if ($(this).valid()) {
        // Client side validation passed => submit the form
    }
});

Another possibility is to hook at the plugin options:

$.validator.setDefaults({
    submitHandler: function(form) {
        // Client side validation passed => submit the form
    }
});

If you are using MSAjax then good luck with this.

这篇关于是否有可能“入侵" ASP.NET MVC 3中支持的有效验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆