Reputation: 5998
I am using MVC 3 and I have the following controller:
[HttpPost]
public ActionResult Suggest(IEnumerable<Connection> connect)
{
return Json(new { result = true });
}
public class Connection
{
public long Id { get; set; }
public string Name { get; set; }
}
My JQuery looks like:
var connections = $('.on');
var connect = [];
$.each(connections, function (i, item) {
var span = $(item);
var id = span.attr('data-entity-id');
var name = span.attr('data-entity-name');
connect.push({ Id: id, Name: name });
});
$.post('myurl', connect, function (data) {
$('.result').html(data);
});
The JSON binding does not work using this code.
Upvotes: 1
Views: 1420
Reputation: 4313
You aren't posting JSON. You need to stringify the data and then tell the server you're sending JSON data. In order to stringify JSON you will need to include Crockford's JSON2 library. (That's the guy who invented JSON.)
$.ajax({
url: "myurl",
type: "POST",
data: JSON.stringify({ connect: connect }),
contentType: 'application/json'
success: function (data) {
$('.result').html(data);
}
});
Also, I think you might need to make your action parameter List<Connection> connect
.
Upvotes: 2