如何强制从Ajax.BeginForm强制回发而不改变不显眼的代码

时间:2014-04-03 10:53:20

标签: jquery asp.net-mvc

我有一个使用Ajax.BeginForm创建的表单。表单提交过滤要求并使用ajax显示结果。

我现在要添加"导出"按钮或将使用完整回发提交该表单的链接,因为该操作会更改响应标头以将内容类型更改为Excel。问题是,不显眼的javascript使用ajax来呈现html。

这是我用来从"导出"提交表单的脚本。链接:

$("#export").click(function () {
    var $form = $(this).closest("form");
    $form.append($("<input type='hidden' name='export' value='true' />"));
    $form.trigger("submit");
    return false;
});

我已经看到了这个问题Ajax.BeginForm force full postback,但我并不热衷于改变不引人注目的剧本

修改 我找到了这种强制回发的技术:

$("#export").click(function () {
    var $form = $(this).closest("form");
    $form.append($("<input type='hidden' name='export' value='true' />"));
    //$form.data("ajax", ""); //didn't work
    $form.attr("data-ajax", "");//but this did
    $form.trigger("submit");
    return false;
});

现在发生的是excel文件的下载,但是原来的html页面被禁用ajax ...

1 个答案:

答案 0 :(得分:1)

我发现这很有用

$("#export").click(function (evt) {
    evt.preventDefault();
    var $form = $(this).closest("form");
    var tempInput = $("<input type='hidden' name='operation' value='export' />");
    $form.append(tempInput);
    $form.attr("data-ajax", "");
    $form.trigger("submit");
    //setTimeout(function () {//thought I might need a timeout to delay the 
                             //reset to after submit but it appears to work 
                             //without
        $form.attr("data-ajax", "true");
        tempInput.remove();
    //}, 0);
});
相关问题