0

I am using asp.net mvc 4, knockout-js within my application. I would like to ask how can i make an call from js file to the controller?

it works when i write this code into the view (razor) page:

<script type="text/javascript">
    $(document).ready(function () {
        var url = '@Url.Action("GetTechnicians", "Ticket")';
        $.post(url, null, function (data) {                    
            alert("get data");
        });
    });     
</script>

now I would like to make the call in *.js file. the problem is that Url.Action is invalid within js file.

My url mapping in global.asax is: "{culture}/{controller}/{action}/{id}", signature of my controller looks like: public JsonResult GetTechnicians()

When I use in js file:

var url = "/Ticket/Technicians";

I get an error: "NetworkError: 404 Not Found - http://localhost/Ticket/Technicians"

I would like to know how to complete the call from js file?

1 Answer 1

1

You could do the following...

In your JS file

var app = {
  urls: {
    getTechnicians: null
  },
  culture: "en",
  getTechnicians: function () {
        if (!app.urls.getTechnicians) {
          throw new Error("getTechnicians URL not set");
        }
        $.post("/" + app.culture + app.urls.getTechnicians, null, function (data) {                    
            alert("get data");
        });
  }
};

In your view

<script type="text/javascript" src="myscriptfile.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
     app.urls.getTechnicians = '@Url.Action("GetTechnicians", "Ticket")';
     app.culture = "en-us";

     // Later on...
     app.getTechnicians();

    });     
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Bart,can you please explain me what is the meanning of the operator ":"
Hi Bart, according to your code i need need to trigger the function outside the js file. send to it the current url. please note that the url.action() gives back the current url "culture/controller/action". there for i think we can use only: post("/"app.urls.getTechnicians,,). we dont need the culture property and it gives an error back: "NetworkError: 404 Not Found - localhost/en/projectName/en/Ticket/GetTechnicians". i have tried: $.post("/" + app.urls.getTechnicians,,), here its fails with no error. its not getting into the function. thank you Ori
@Ori The Url.Action only works in views. If you want your code in a separate JS file you need to pass in the URL. From then on it doesn't matter if you call your function from the page or from your JS file. The other option is to pass the url to the function that does the Ajax call, but then you need to know upfront when and where you call it. My solution requires that you register the URL(s) once and then you can call the function as many times and from anywhere. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.