HTML Button的jQuery函数无法使用MVC FileResult [英] HTML Button's jQuery function is having trouble with MVC FileResult

查看:60
本文介绍了HTML Button的jQuery函数无法使用MVC FileResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算让此函数调用我的MVC操作方法,该方法返回CSV报告.

I intended for this function to call my MVC action method that returns a CSV report.

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

如果我按以下代码创建一个按钮,则单击该按钮后,将显示打开方式/保存文件"窗口.

If I make a button like the code below then, when it is clicked, I'm given the "Open with/Save File" window.

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

但是,当我更改按钮以使用我的jQuery函数时,尽管调用了GetCSVReport(),但没有显示打开/保存文件"窗口.

However, when I change my button to use my jQuery function then, although GetCSVReport() is called I'm not given the "Open with/Save File" window.

这是我的GetCSVReport()

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

如何使jQuery函数像onClick一样工作?

How can I get my jQuery function to work like the onClick?

谢谢

亚伦

推荐答案

使用之前使用的相同代码:

Use the same code you used before:

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

get触发一个Ajax调用,您在这里不需要.您使用jQuery绑定事件,但操作保持不变.

get triggers an Ajax call, which you do not want here. You use jQuery to bind the event, but the action stays the same.

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

To add query string parameters, use:

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

这篇关于HTML Button的jQuery函数无法使用MVC FileResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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