简体   繁体   中英

C# Leaflet how do i properly post data from view to controller using ajax?

currently i keep getting the alert request failed whenever i try to pass back data from view to controller.

leafletLats and leafletLngs are an array, but im not sure of the datatype. They are derived from coord which is of type LatLng[].

may i know what is causing the Post method to not pass through? is it the problems in the controller?

in View

routeControl.on('routeselected', function (e) {
            var coord = e.route.coordinates;
            var name = e.route.name;
            var leafletLats = coord.map(function(point) {
                return [point.lat];
            });
            var leafletLngs = coord.map(function(point) {
                return [point.lng];
            });

            alert('Array size = ' + coord.length + '\nCoordinates: \n' + leafletLats);
            alert('Array size = ' + coord.length + '\nCoordinates: \n' + leafletLngs);
            //alert('Array size = ' + coord.length + '\nCoordinates: \n' + coord.join('\n'));

            $.ajax({ 
                type: 'Post',
                url: '/Map/GetRouteCoordinates',
                data: JSON.stringify(leafletLats),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) { 
                    alert(`Request passed!`);
                    console.log(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(`Request failed!`);
                }
            });

in controller

[HttpPost]
        public ActionResult GetRouteCoordinates(MyModel test)
        {
            //do something with the result
            Debug.WriteLine("data passed back");



            return View();
        }


    public class MyModel
    {
        public List<MyModel_Value> latList { get; set; }
    }

    public class MyModel_Value
    {
        public double lat { get; set; }        
    }

做了一些修改并将数据更改为data: JSON.stringify({ arr: stringifiedArr })对我data: JSON.stringify({ arr: stringifiedArr })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM