简体   繁体   English

MVC相当于@ Html.ActionLink通过javascript

[英]MVC equivalent of @Html.ActionLink through javascript

如何通过javascript代码编写等效的@ Html.ActionLink,即调用MVC操作,然后创建一个新视图而不返回调用视图?

javascript is a client side language which doesn't know anything about the server side language you are using. javascript是一种客户端语言,它对您使用的服务器端语言一无所知。 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. 因此,使用服务器端路由定义生成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,你无法访问服务器端助手,你仍然可以根据情况在一些DOM元素中包含这个url。

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. 请注意我们嵌入到DOM中的data-url HTML5属性,并使用服务器端帮助程序来确保生成的URL始终基于我们的路由定义是正确的。 Now we could in a separate javascript file unobtrusively subscribe to the click event of this div and retrieve the url: 现在我们可以在一个单独的javascript文件中不引人注意地订阅此div的click事件并检索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文件中所要做的就是获取其相应的hrefaction属性以检索实际的URL和用它做点什么。

Another option is to store the URL in a hidden <div> somewhere on your page and call it via Javascript later. 另一种选择是将URL存储在页面某处隐藏的<div> ,稍后通过Javascript调用它。 It would look like this: 它看起来像这样:

Index.cshtml : Index.cshtml

<div style="display: none;" id="url">
   @Url.Action("SomeAction", "SomeController")
</div>

Then you can use this in your Script.js file 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的网络安全链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM