Tuesday, October 14, 2014

Array and Model to MVC Controller Method via jQuery Post()

Posting this as a reminder on how to make the jQuery post() method work with MVC when passing an array.


submitRequest: function () {
$.blockUI();
var self = model;
var selectedIds = []
$.each(self.items, function (i, o) {
if (o.isSelected()) {
selectedIds.push(o.id);
}
})
var email = self.isEmailSelected() ? self.Email() : "";
var sms = self.isSmsSelected() ? self.PhoneNumber() : "";
$.post("/Controller/Method", { 'ids[]': selectedIds, id: self.Id, email: email, sms: sms }, function () {
// success code
}).fail(function () {
// Error code
})
}
view raw gistfile1.js hosted with ❤ by GitHub