0

I have some js code, that I would like to move to an external js file, but the script references razor syntax (url path) in the View.

Here is the script:

 function getStops() {
    var url = $('#map').data('request-url');
    $.getJSON(url,
        function (data) {
            var marker = [];

            $.each(data,
                function (i, item) {
                    marker.push({
                        'location': new google.maps.LatLng(item.Latitude2, item.Longitude2),
                        'map': map,
                        'weight': item.Difference,
                        'radius': 10
                    });
                });
            var pointArray = new google.maps.MVCArray(marker);

            heatmap = new google.maps.visualization.HeatmapLayer({
                data: pointArray
            });
            heatmap.setMap(map);
        });
};

// get Driving and show on layer
function getDriving() {
    var url = $('#map').data('request-url2');
    $.getJSON(url,
        function (data) {
            var marker = [];

            $.each(data,
                function (i, item) {
                    marker.push({
                        'location': new google.maps.LatLng(item.Latitude2, item.Longitude2),
                        'map': map,
                        'weight': item.Speed,
                        'radius': 10
                    });
                });
            var pointArray = new google.maps.MVCArray(marker);

            heatmap = new google.maps.visualization.HeatmapLayer({
                data: pointArray
            });
            heatmap.setMap(map);
        });

I know about data-request-url that I can reference in an element (in my case it's a div).

So I wrote it like this in the View:

<div id="map" data-request-url="@Url.Action("GetStops", "Home")" data-request-url2="@Url.Action("Driving", "Home")">

Is this the right way to go, or can it be done in other way?

RickL
  • 3,318
  • 10
  • 38
  • 39
S.E
  • 107
  • 13
  • https://stackoverflow.com/questions/7902213/asp-net-mvc-razor-symbol-in-js-file it might be helpful – Bhanu Tharun Oct 25 '17 at 09:36
  • I saw this answer and I do code according to it. My question is this right way? Or I can do it more right@BhanuTharun – S.E Oct 25 '17 at 09:38

0 Answers0