如何在@URL.Action() 中访问 javascript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6456703/
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 access javascript variable within @URL.Action()
提问by Bolu
How can I access JavaScript value inside @URL.Action()
?
something like:
如何访问里面的 JavaScript 值@URL.Action()
?就像是:
<script type="text/javascript">
function name(myjavascriptID)
{
jQuery("#list_d").jqGrid('setGridParam', { url: '@URL.Action("download file", "download", new { id = <myjavascriptID> })', page: 1 });
}
</script>
回答by Brian Mains
You can't. JavaScript doesn't execute when generating the action URL. What you can do, is do something like this:
你不能。生成操作 URL 时不执行 JavaScript。你可以做的是做这样的事情:
function name(myjavascriptID) {
var link = '@Url.Action("download file", "download", new { id = "-1" })';
link = link.replace("-1", myjavascriptID);
jQuery("#list_d").jqGrid('setGridParam', { url: link, page: 1 });
}
回答by nocarrier
I do something fairly similar, but less verbose:
我做了一些非常相似但不那么冗长的事情:
var myUrl = '@Url.Action("Solution","Partner")/' + myjavascriptID;
$.ajax.load(myUrl); // or whatever
We can do this because of routing, and ultimately Url.Action with route dictionary parameters translates into a URI that looks like:
由于路由,我们可以这样做,最终带有路由字典参数的 Url.Action 转换为一个 URI,如下所示:
http://localhost:41215/Partner/Solution?myJavascriptID=7
Just a second choice, because as a wise old man once said "It is our choices, Harry, that show what we truly are, far more than our abilities."
只是第二个选择,因为正如一位聪明的老人曾经说过的那样,“这是我们的选择,哈利,展示了我们的真实身份,远远超过我们的能力。”
回答by games
You can pass in the variables to any link as shown below...
您可以将变量传递到任何链接,如下所示...
var url = '@Html.Raw(@Url.Action("MethodName", "ControllerName"))' + '?id = ' + myjavascriptID
回答by actaram
In the same vein as Brian Mains's answer, you could format your url string instead of replacing -1 with your variable, that is if like me, you judge it is better to read. The following answer assumes that you've modified String
's prototype as suggested in this answer:
与 Brian Mains 的回答一样,您可以格式化您的 url 字符串,而不是将 -1 替换为您的变量,也就是说,如果像我一样,您认为阅读会更好。以下答案假定您已String
按照本答案中的建议修改了原型:
var url = unescape('@Url.Action("download file", "download", new { id = "{0}" })').format(myjavascriptID);
The unescape
call is necessary if you want to decode your {0}
. I like this alternative, because it makes it easier to have multiple parameters from JS variables. For instance:
的unescape
,如果你想你的电话解码是必要的{0}
。我喜欢这个替代方案,因为它可以更容易地从 JS 变量中获得多个参数。例如:
var url = unescape('@Html.Raw(Url.Action("Action", "Controller", new { id = "{0}", name = "{1}" }))').format(myID, myName);
I added Html.Raw
in my second example in order to avoid having &
in the url string.
我Html.Raw
在我的第二个例子中添加了以避免&
在 url 字符串中出现。
回答by Joh
You can replace url like code below
您可以像下面的代码一样替换 url
var jsUrl = '@Url.Action("action", "controller")'; // ## is the token
var getUrl = jsUrl.replace('action', action).replace('controller', controller)
$("#RenderPartial").load(getUrl);
回答by xMaa
You can build the JavaScript code in C# this way:
您可以通过以下方式在 C# 中构建 JavaScript 代码:
function fun(jsId) {
var aTag = '<a href="@Html.Raw(@Url.Action("ActionName", "ControllerName", new { objectId = "xxx01xxx" }).Replace("xxx01xxx", "' + jsId + '"))">LINK</a>';
}
Here is the code from the question rewritten using this technique:
以下是使用此技术重写的问题中的代码:
function name(myjavascriptID) {
jQuery("#list_d").jqGrid('setGridParam', { url: '@Html.Raw(@URL.Action("download file", "download", new { id = "XXXmyjavascriptIDXXX" }).Replace("XXXmyjavascriptIDXXX", "' + myjavascriptID + '"))', page: 1 });
}