0

Can someone suggest how to use @URL.Action( "name", "controller", parameters} in your Jquery? Or atleat get the relative root to home directory.

I have to use JQuery get using the following code.

 $(".ag").click(function () {
    var id = this.id.replace("message_", "");
    var cd = this.id.replace("message_", "display_message_");
    $.get("/MRN/_Details_Message", function (data, status) {
        document.getElementById(cd).innerHTML = "Data: " + data + "\nStatus: " + status;
    });
});

The problem is that you cannot use @URL.Action if your JS file is separate and if you google examples there are always complaints when using relative HTML paths with MVC.

Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79
  • You **can** pass url in separate js file from view: [look answer here](http://stackoverflow.com/questions/17394880/getting-rid-of-hardcoded-strings-in-javascript-with-asp-net-mvc/17396877#17396877) – YD1m Jul 08 '13 at 20:31

5 Answers5

3

An easy solution is to put the url as an attribute of the relevant html element.

<a href="#" class="ag" data-url="@Url.Action("ActionName")">Click Here</a>

From within your jQuery code you can reference this easily:

$(".ag").click(function (e) {
    var url = $(this).data("url");
});
ZippyV
  • 12,540
  • 3
  • 37
  • 52
  • This wouldn't be possible for dynamic elements. For example if my js adds a new anchor tag the Razor will not get rendered as html. – edhedges Nov 26 '13 at 17:48
1

I created a very simple MVC Controller to expose Routes that you need to access from javascript. You can find it at my GitHub -> https://github.com/tucaz/JSRouteController

tucaz
  • 6,524
  • 6
  • 37
  • 60
1

I've ran into similar issues. If your JavaScript is in a different file (which I'm assuming), I just created a global function that kind of takes care of it, that is if you know the part of the relative path:

function getPath(p) {
    var path = "";
    if (window.location.href.indexOf("/MRN") > -1) {
        path = "/MRN";
    }
    if (path.indexOf("/", path.length - 1) !== -1) {
        return path + p;
    } else {
        return path + "/" + p;
    }
}

Probably not the best solution, but it works for my projects.

Justin Chmura
  • 1,939
  • 1
  • 15
  • 17
0

One approach is to pass url as parameter and render all interesting Urls as part of the page. Something like

On the page:

...var detailsUrl = '@Url.Action(....)';

In JavaScript:

  $(".ag").click(function () {
    var resultSelector = this.data("resultselector");
    $.get(detailsUrl , function (data, status) {
        $(resultSelector).html("Data: " + data + "\nStatus: " + status);
    });
});
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

I like to do the following, assuming "MRN" is your controller and "_Details_Message" is your action:

$.get('@Url.Action("_Details_Message", "MRN")', function (data, status) {
    document.getElementById(cd).innerHTML = "Data: " + data + "\nStatus: " + status;
});

This will embed the root action into your javascript as "/MRN/_Details_Message" with the proper root prepended.

Matt Houser
  • 33,983
  • 6
  • 70
  • 88