0

Is this the proper way to reference a controller from this jquery?

   $.get("@Url.Action("Json_OrderTypes", "Order")", function (data) {

vs.

 $.get("../Order/Json_OrderTypes", function (data) {
user1929393
  • 4,089
  • 7
  • 30
  • 48
  • 3
    Depend how you define _proper_. Its a good way because it gives you the benefits of the inbuilt route checks performed by the `@Url.Action()` method (and personally its the only way I would do it) –  Dec 01 '14 at 03:20
  • 1
    first one is always preffered.. as it is useful in many cases see:http://stackoverflow.com/questions/27147346/mvc-why-use-actionlink-instead-of-hard-coding-the-link – Ehsan Sajjad Dec 01 '14 at 05:46

1 Answers1

0

Yes, the first way is preferred. Some folks like to keep virtually all JavaScript that they can in separate files. If that's the case you can do something slightly different, which stores the values and initialized javascript objects with the values. The purists will do this 100% javascript free and use data- attributes (and other means) also discussed there but for ex

do you write your JavaScript in a ASP.NET MVC view

<div class="text-widget" 
     data-options="@Json.Encode(new { url = Url.Action("Update", "Text", new { id = 3 }), uselessParam = true })">
  <input type="text" />
</div>
Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71