简体   繁体   中英

@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?

localhost:53720/@Url.Action(Edit,%20Contacts)?id=23918

Edition: Actually the route generated for that code is

localhost: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

<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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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