Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  134] [ 1]  / answers: 1 / hits: 45220  / 9 Years ago, wed, march 4, 2015, 12:00:00

In a AJAX request to the server in MVC, how can I pass a list of id's to the controller's action function?



I accept with or without use of Html helpers.



I know MVC's model binder has no problem when it comes to simple types like int, string and bool.



Is it something like I have to use and array instead in the action?



I don't care if I have to use an array or List and even if the strings I int or strings I can always convert them. I just need them on the server.
My List ids gives null at the moment.



Javascript:



var ids= [1,4,5];
// ajax request with ids..


MVC Action:



public ActionResult ShowComputerPackageBuffer(List<int> ids) // ids are null
{
// build model ect..
return PartialView(model);
}


EDIT: Added my AJAX request



$(document).ready(function () {
$('#spanComputerPackagesBuffer').on('click', function () {
var ids = $('#divComputerPackagesBuffer').data('buffer');
console.log('bufferIds: ' + bufferIds);
var data = {
ids: ids
};

var url = getUrlShowComputerPackageBuffer();
loadTable(url, result, data);
});
});

// AJAX's
function loadTable(url, updateTargetId, data) {
var promise = $.ajax({
url: url,
dataType: html,
data: data
})
.done(function (result) {
$('#' + updateTargetId).html(result);
})
.fail(function (jqXhr, textStatus, errorThrown) {
var errMsg = textStatus.toUpperCase() + : + errorThrown + '. Could not load HTML.';
alert(errMsg);
});
};

// URL's
function getUrlShowComputerPackageBuffer() {
return '@Url.Action(ShowComputerPackageBuffer, Buffer)';
};


SOLUTIONS: // Thanks to @aherrick comment. I missed the good old traditional



$.ajax({
type: POST,
url: '@Url.Action(ShowComputerPackageBuffer, Buffer)',
dataType: json,
traditional: true,
data: {
bufferIds: bufferIds
}
});

More From » ajax

 Answers
2

Use the traditional parameter and set it to true.



$.ajax({
type: POST,
url: /URL,
dataType: json,
traditional: true,
data: {}
});

[#67576] Monday, March 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;