简体   繁体   English

$。ur中的@ Url.Action

[英]@Url.Action inside $.get

I've seen a few posts about this but stills couldn't understand why this is not working 我已经看过一些关于这个的帖子但是仍然无法理解为什么这不起作用

$.get('@Url.Action("Edit","Contacts")', {id: parseInt($(this).attr('id')) } , function (result) {

obviously this does 显然这样做

$.get("/Contacts/Edit/" + parseInt($(this).attr('id')), function (result) {

I've tried with replacements and still getting the proper id but the @Url.Actions appears as a string itself generating weird routes as this one for the former code, seems to me that the url.action is not executed, right? 我已尝试使用替换但仍然获得正确的ID,但@ Url.Actions显示为字符串本身生成奇怪的路由,因为前一代码的这个,在我看来,url.action没有被执行,对吧?

localhost:53720/@Url.Action(Edit,%20Contacts)?id=23918 本地主机:53720/@Url.Action(编辑%20Contacts)ID = 23918

Edition: Actually the route generated for that code is 版本:实际上为该代码生成的路由是

localhost:53720/Url.Action(%22Edit%22,%20%22Contacts%22)?id=23918 本地主机:53720 / Url.Action(%22Edit%22%20%22Contacts%22)ID = 23918?

the other is for another try I've made 另一个是我做的另一次尝试

Anyone can tell me why? 谁能告诉我为什么?

Thanks 谢谢

To have access to HtmlHelpers inside javascript, the javascript code must be inside a View page, and not inside a javascript file 要在javascript中访问HtmlHelpers,javascript代码必须位于View页面内,而不是在javascript文件中

<script>
    var url = '@Url.Action("Edit","Contacts")';
    console.log(url);
</script>

Looks like @Url.Action is not getting called and the URL is not being returned. 看起来@Url.Action没有被调用,并且没有返回URL

var url = '@Url.Action("Edit","Contacts")';

$.get(url, {id: parseInt($(this).attr('id')) } , function (result)

If JS complains about the above, you can try specifying @: beforehand to let razor take control. 如果JS抱怨上述内容,你可以尝试指定@: beforehand让razor控制。

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

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