【问题标题】:HTML Button's jQuery function is having trouble with MVC FileResultHTML Button 的 jQuery 函数在使用 MVC FileResult 时遇到问题
【发布时间】:2011-02-02 13:03:07
【问题描述】:

我打算让这个函数调用返回 CSV 报告的 MVC 操作方法。

$(function() {
    $('#exportButton').click(function() {
        $.get('/curReport/GetCSVReport');
    });
});

如果我像下面的代码那样制作一个按钮,当它被点击时,我会看到“打开方式/保存文件”窗口。

<input type="button" value="Export" onClick="location.href='CurReport/GetCSVReport'">

但是,当我将按钮更改为使用 jQuery 函数时,虽然调用了 GetCSVReport(),但我没有得到“打开方式/保存文件”窗口。

这是我的GetCSVReport()

public FileResult GetCSVReport()
{
    ...
    return fileResult;
}

如何让我的 jQuery 函数像 onClick 一样工作?

谢谢,

亚伦

【问题讨论】:

    标签: javascript jquery model-view-controller


    【解决方案1】:

    使用与之前相同的代码:

    $(function() {
        $('#exportButton').click(function() {
            location.href = 'CurReport/GetCSVReport';
        });
    });
    

    get 触发 Ajax 调用,您不希望在此处这样做。您使用 jQuery 绑定事件,但操作保持不变。

    要添加查询字符串参数,请使用:

    location.href = 'CurReport/GetCSVReport?filter=' + escape(val);
    

    【讨论】:

    • 如何为参数添加值?使用旧方法我可以完成这个“$.get('/curReport/GetCSVReport', {filter: val});”。谢谢!
    • @Aaron - 更新了答案。如果val 有一个简单的值(例如,数字),则不必对其进行转义。
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多