如何在点击时从JavaScript添加参数到@ Url.Action()调用 [英] How to add parameters to a @Url.Action() call from JavaScript on click

查看:88
本文介绍了如何在点击时从JavaScript添加参数到@ Url.Action()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接,当点击时需要使用必须通过JavaScript检索的某些数据调用控制器操作。该操作将返回一个FileStreamResult。

I have a link that when clicked needs to call a controller action with certain data which must be retrieved via JavaScript. The action will be returning a FileStreamResult.

我看了@ Url.Action但是我无法弄清楚我是如何(或者甚至)如何传递值字典的东西必须通过JS检索。

I looked at @Url.Action but I couldn't figure out how (or even if) I could pass value dictionary stuff which had to be retrieved via JS.

然后我从点击处理程序中获取$ .post。我遇到的问题是我不知道该怎么做才能成功:function()将文件流结果返回给用户。或者即使我可以。

So then I went with a $.post from a click handler. The problem I'm having is that I'm not sure what to do in my success: function() to return the file stream result to the user. Or even if I can.

所以关于你如何做这样的事情的任何帮助都会很棒..

So any help on how you would do something like this would be great..

推荐答案


然后我从点击处理程序中获取$ .post。我遇到的问题是我不知道该怎么做才能成功:function()将文件流结果返回给用户。或者即使我可以。

So then I went with a $.post from a click handler. The problem I'm having is that I'm not sure what to do in my success: function() to return the file stream result to the user. Or even if I can.

完全正确。你不能对javascritpt中的接收字节做很多事情:显然你不能将它保存在客户端计算机上,也不能将它传递给客户端上的某个外部程序。因此,不要调用应该使用AJAX返回文件的操作。对于这些操作,您应该使用普通链接:

Exactly. You can't do much with a received byte in javascritpt: obviously you cannot save it on the client computer nor pass it to some external program on the client. So don't call actions that are supposed to return files using AJAX. For those actions you should use normal links:

@Html.ActionLink("download file", "download", new { id = 123 })

并让用户决定如何处理该文件。您可以使用 Content-Disposition 标头进行播放,并将其设置为内联附件取决于您是希望使用浏览器内的默认关联程序打开文件,还是使用保存文件对话框提示用户。

and let the user decide what to do with the file. You could play with the Content-Disposition header and set it to either inline or attachment depending on whether you want the file to be opened with the default associated program inside the browser or prompt the user with a Save File dialog.

更新:

似乎我误解了这个问题。如果要将参数附加到现有链接,可以在javascript中订阅click事件,并通过将必要参数附加到查询字符串来修改href:

It seems that I have misunderstood the question. If you want to append parameters to an existing link you could subscribe for the click event in javascript and modify the href by appending the necessary parameters to the query string:

$(function() {
    $('#mylink').click(function() {
        var someValue = 'value of parameter';
        $(this).attr('href', this.href + '?paramName=' + encodeURIComponent(someValue));
        return true;
    });
});

这篇关于如何在点击时从JavaScript添加参数到@ Url.Action()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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