MVC 等效于 @Html.ActionLink 通过 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10984394/
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
MVC equivalent of @Html.ActionLink through javascript
提问by Martin
How do I code the equivalent of an @Html.ActionLink through javascript code i.e. call an MVC action which will then create a new View without coming back to the calling view?
如何通过 javascript 代码编写等效于 @Html.ActionLink 的代码,即调用 MVC 操作,然后该操作将创建一个新视图而不返回调用视图?
回答by Darin Dimitrov
javascript
is a client side language which doesn't know anything about the server side language you are using. Thus it is normal that there's no equivalent in javascript of the server side helper that generates an url using your server side route definitions.
javascript
是一种客户端语言,它对您使用的服务器端语言一无所知。因此,在使用服务器端路由定义生成 url 的服务器端助手的 javascript 中没有等效项是正常的。
It is not quite clear what you are trying to achieve but if you want to use call some url through javascript you could generate this url using a server side helper:
目前还不清楚您要实现的目标,但是如果您想通过 javascript 调用一些 url,您可以使用服务器端帮助程序生成此 url:
<script type="text/javascript">
var url = '@Url.Action("SomeAction", "SomeController")';
// do something with the url client side variable, for example redirect
window.location.href = url;
</script>
If you want to use this url in a separate javascript file where you don't have access to server side helpers you could still, depending on the situation, include this url in some DOM element.
如果您想在无法访问服务器端帮助程序的单独 javascript 文件中使用此 url,您仍然可以根据情况将此 url 包含在某些 DOM 元素中。
For example:
例如:
<div id="foo" data-url="@Url.Action("SomeAction", "SomeController")">Click me</div>
Notice the data-url
HTML5 attribute that we have embedded into the DOM and used a server side helper to ensure that the generated url will always be correct based on our routing definitions. Now we could in a separate javascript file unobtrusively subscribe to the click event of this div and retrieve the url:
请注意data-url
我们嵌入到 DOM 中的HTML5 属性,并使用了服务器端帮助器来确保生成的 url 根据我们的路由定义始终是正确的。现在我们可以在一个单独的 javascript 文件中不显眼地订阅这个 div 的点击事件并检索 url:
$('#foo').click(function() {
var url = $(this).data('url');
// do something with the url client side variable, for example redirect
window.location.href = url;
});
Other examples obviously include the standard <a>
and <form>
elements which should be generated using server side HTML helpers and then all you have to do in your separate javascript file is to fetch their corresponding href
or action
attributes to retrieve the actual url and do something with it.
其他示例显然包括应该使用服务器端 HTML 帮助程序生成的标准<a>
和<form>
元素,然后您在单独的 javascript 文件中要做的就是获取它们的对应href
或action
属性以检索实际 url 并对其进行处理。
回答by MSOACC
Another option is to store the URL in a hidden <div>
somewhere on your page and call it via Javascript later. It would look like this:
另一种选择是将 URL 存储在<div>
页面上的隐藏位置,稍后通过 Javascript 调用它。它看起来像这样:
Index.cshtml:
索引.cshtml:
<div style="display: none;" id="url">
@Url.Action("SomeAction", "SomeController")
</div>
Then you can use this in your Script.jsfile to build any links you want.
然后你可以在你的Script.js文件中使用它来构建你想要的任何链接。
var url = $("#url").text();
The URL variable would then contain a websafe link to /SomeController/SomeAction
.
URL 变量随后将包含指向 的网络安全链接/SomeController/SomeAction
。