Post JavaScript array with AJAX to MVC controller

You could define a view model:

public class AddUserViewModel
    public int ProjectId { get; set; }
    public int[] userAccountIds { get; set; }

then adapt your controller action to take this view model as parameter:

public ActionResult AddUsers(AddUserViewModel model)

and finally invoke it:

function sendForm(projectId, target) {
        url: target,
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ 
            projectId: projectId, 
            userAccountIds: [1, 2, 3] 
        success: ajaxOnSuccess,
        error: function (jqXHR, exception) {
            alert('Error message.');

Leave a Comment