0

How can I use Razor in a file with the extension js? For example:

 function Tracker($elem, prodn) {
     var url = '/BackEndServices/StartTracking'; 
     if ($elem.hasClass('Tracked')) {
          url = '/BackEndServices/StopTracking';
     } $.ajax({
         url: url,
         type: 'POST',
         data: { prodN: prodn, nprt: $elem.attr('nprt') },
         success: function (result) { $elem.closest('span').html(result); $("[rel=tooltip]").tooltip({ html: true }); },
     });
 }

I would like to use the method Url.Action.is it possible?

Joel Cochran
  • 7,139
  • 2
  • 30
  • 43
Ts-alan
  • 47
  • 2
  • 6
  • No, but you can do something like this, http://stackoverflow.com/questions/13640559/asp-net-mvc-url-action-in-external-js-file or declare settings variable in your master – Yaroslav Bigus Jul 22 '14 at 08:09

2 Answers2

0

You can pass this Url through some global variable. For example write in your cshtml file:

<script>
    $.serverData = { startUrl: @Url.Action("Start"), stopUrl: @Url.Action("Stop") };
</script>

And after that use it in your js file:

 function Tracker($elem, prodn) {
     var url = $elem.hasClass('Tracked') ? $.serverData.StopUrl : $.serverData.StartUrl;
     $.ajax({
         url: url,
         type: 'POST',
         data: { prodN: prodn, nprt: $elem.attr('nprt') },
         success: function (result) { $elem.closest('span').html(result); $("[rel=tooltip]").tooltip({ html: true }); },
     });
Pavel Bakshy
  • 7,697
  • 3
  • 37
  • 22
0

One of options -to put a script in Partial View

Ts-alan
  • 47
  • 2
  • 6