Ajax.BeginForm强制全回发 [英] Ajax.BeginForm force full postback

查看:89
本文介绍了Ajax.BeginForm强制全回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制Ajax.BeginForm(MVC3,不显眼)进行完整的回发?

How do I force Ajax.BeginForm (MVC3, unobtrusive) to do a full postback?

在MVC2中,我刚刚清除了 onclick和 onsubmit处理程序。我该如何在MVC3中执行此操作?

In MVC2 I just cleared the "onclick" and "onsubmit" handlers. How do I do this in MVC3?

//does not work in MVC 3 with unobtrusive ajax :((
$('#myForm').unbind('click');
$('#myForm').unbind('submit');

PS。如果您有兴趣,我需要此文件进行上传。如果没有文件正在上传-ajax很好。如果文件正在上传-让它来做完整的回发。

PS. In case you're interested, I need this for file uploads. If no file is being uploaded - ajax is fine. If a file is being uploaded - let it do a full postback.

更新:

查看 jquery.unobtrusive -ajax.js源代码,我看到了这一点:

After looking at "jquery.unobtrusive-ajax.js" source code I saw this:

$("form[data-ajax=true]").live("submit", blahblah);

所以我想出了这个方法,它可行:

So I came up with this and it works:

$("form[data-ajax=true]").die("submit");

但这似乎有点 hacky,从表单中清除所有onsubmit事件的任何合法方式是吗?我想更新的问题是现在:如何使用使用不同选择器的 die语句清除实时事件处理程序?

But this seem a little "hacky", any "legitimate" way to clear all onsubmit events from a form? I guess the updated question is now: How do I clear a "live" event-handler with a "die" statement that uses a different selector?

推荐答案

如果可以修改不引人注意的库,则可以..您可以在submit事件处理程序中检查表单是否包含任何文件,如果是,则

If you are fine with modifying the unobtrusive library.. you can check in the submit event handler whether the form contains any file and if yes then do a full POST else through AJAX.

$("form[data-ajax=true]").live("submit", function (evt) {
    var clickInfo = $(this).data(data_click) || [];
    evt.preventDefault();
    if (!validate(this)) {
      return;
    }

    if ($("input[type=file]", this).val()) {
      this.submit();
    }
    else {
      asyncRequest(this, {
        url: this.action,
        type: this.method || "GET",
        data: clickInfo.concat($(this).serializeArray())
      });
    }
  });

这篇关于Ajax.BeginForm强制全回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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