如何在单击时从 JavaScript 向 @Url.Action() 调用添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5332223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add parameters to a @Url.Action() call from JavaScript on click
提问by Shane Courtrille
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.
我有一个链接,单击该链接时需要调用具有某些必须通过 JavaScript 检索的数据的控制器操作。该操作将返回一个 FileStreamResult。
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.
我查看了@Url.Action,但我不知道如何(或什至是否)我可以传递必须通过 JS 检索的值字典内容。
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.
然后我从点击处理程序中获取了 $.post 。我遇到的问题是,我不确定成功后该怎么做: function() 将文件流结果返回给用户。或者即使我可以。
So any help on how you would do something like this would be great..
所以任何关于你如何做这样的事情的帮助都会很棒..
回答by Darin Dimitrov
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.
然后我从点击处理程序中获取了 $.post 。我遇到的问题是,我不确定成功后该怎么做: function() 将文件流结果返回给用户。或者即使我可以。
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:
确切地。你不能用 javascritpt 接收到的字节做太多事情:显然你不能将它保存在客户端计算机上,也不能将它传递给客户端上的某个外部程序。所以不要调用应该使用 AJAX 返回文件的操作。对于这些操作,您应该使用普通链接:
@Html.ActionLink("download file", "download", new { id = 123 })
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.
并让用户决定如何处理文件。您可以使用Content-Disposition
标题并将其设置为inline
或attachment
取决于您是否希望使用浏览器内的默认关联程序打开文件或使用“保存文件”对话框提示用户。
UPDATE:
更新:
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:
我似乎误解了这个问题。如果要将参数附加到现有链接,您可以在 javascript 中订阅 click 事件并通过将必要的参数附加到查询字符串来修改 href:
$(function() {
$('#mylink').click(function() {
var someValue = 'value of parameter';
$(this).attr('href', this.href + '?paramName=' + encodeURIComponent(someValue));
return true;
});
});
回答by WorldIsRound
Instead of going with a post, I'd go with associate a JQuery on click handler of the link which would call the controller action. This is assuming that the action method returns a FileStreamResult and sets the correct content type so that the browser interprets the result and renders it accordingly.
我不会使用帖子,而是在链接的单击处理程序上关联一个 JQuery,该链接将调用控制器操作。这是假设 action 方法返回 FileStreamResult 并设置正确的内容类型,以便浏览器解释结果并相应地呈现它。
With your approach you'd have to interpret in the onSuccessHandler of the post on how to render the generated stream.
使用您的方法,您必须在帖子的 onSuccessHandler 中解释如何呈现生成的流。