2

I'm struggling around a wired JSON Problem. I'm working with ASP C# and have a View sending a JS-Array to a Controller Action.

Here is the Structure of the JSON Data.

public class MapData
{
    public List<Point> Points { get; set; }
}

public class Point
{
    public string lat { get; set; }
    public string lng { get; set; }
}

Here's the Routine I use to generate the JSON Array: for test purposes it doesn't generate valid MapData.

$("#pac-save").click(function () {
    var mapPoints = {
        Points: []
    }

    mapPoints.Points.push({
        lat:"asdf1",
        lng:"asdf1"
    })
    mapPoints.Points.push({
        lat:"asdf2",
        lng:"asdf2"
    })    
    $.ajax({
        url: "/Map/saveGeplanteRoute",
        data:mapPoints
    }).done(function (dd) {
        alert("done");
    }).fail(function () {
        alert("error");
    });
})

Here's the Controller Action

public JsonResult saveGeplanteRoute(MapData data)
{
    var test = new MapData
    {
        Points = new List<Point>()
    };
    test.Points.Add(new Point() { lat = "asdf1", lng = "asdf1" });
    test.Points.Add(new Point() { lat = "asdf2", lng = "asdf2" });
    return Json(test);
}

The weird thing: When I stringify the data before sending it, it looks like this:

"{"Points":[{"lat":"asdf1","lng":"asdf1"},{"lat":"asdf2","lng":"asdf2"}]}"

The data coming back from Controller looks exactly the same when I stringify it: Why can't C# parse the Point data? The amount of point data is right as you can see below:

JSON data

I'll be glad on some advice pointing me to an error I did not found

2
  • What does your Point class look like? Commented Apr 29, 2016 at 14:21
  • Just noticed it. Thanks. Commented Apr 29, 2016 at 14:22

3 Answers 3

4

change HTTP method to POST from GET

$.ajax({
        url: "/Map/saveGeplanteRoute",
        type: "POST",
        data: mapPoints
    }).done(function (dd) {
        alert("done");
    }).fail(function () {
        alert("error");
    });

GET method sends data using URL string which is not JSON friendly

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much..that did the Trick
0

In the javascript where you generate the test data, put he lat and lng names between quotes: mapPoints.Points.push({ "lat":"asdf1", "lng":"asdf1" })

Comments

0

If you don't set the method on the $.ajax call yor issuing an http GET, and serializing the array in the url, that way you will have problems with the url length if you try to serialize a big array.

A better way could be send a post call, and serialize the array on the body of the call, this can be done that way

$.ajax({
    url: "/home/saveGeplanteRoute",
    data: mapPoints,
    method: 'POST'
})
.done(function (dd) {
     alert("done");
}).fail(function () {
     alert("error");
});

Comments

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.