4

I have a simple javascript array. it's declared like this:

coords = []

and everytime a user clicks on an image, I do something like this:

coords.push([x,y])

so I end up with something like this: (in javascript)

[[342,144],[477,99],[632,148],[529,162]]

but I don't know what to bind it to in the controller method... I've tried

List<List<int>>, int[][], int[,]

none of them seem to work. It only works when I use string.

This is the code I'm using to send it to the server:

$.ajax({
    type: "POST",
    url: "/home/SaveCoords",
    data: { coords: JSON.stringify(coords) }
}).done(function (msg) {
    alert("Data Saved: " + msg);
});

And this is the code I use on the controller

[HttpPost]
public ActionResult SaveCoords(string coords)
{
    return Json("Hello", JsonRequestBehavior.AllowGet);
}

Help?

1
  • Have you tried List<Tuple<int,int>>? Commented Apr 27, 2012 at 17:05

1 Answer 1

6

You should set request content type to application/json, and change data accordingly.

This is the working example:

    $.ajax({
        type: "POST",
        url: "/home/SaveCoords",
        contentType : 'application/json',
        data: JSON.stringify(coords)
    }).done(function (msg) {
        alert("Data Saved: " + msg);
    });

And server

    public ActionResult SaveCoords(int[][] coords)
    {
        return View();
    }
Sign up to request clarification or add additional context in comments.

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.