forum

Home / DeveloperSection / Forums / Post JavaScript array with AJAX to asp.net MVC controller

Post JavaScript array with AJAX to asp.net MVC controller

Manoj Bhatt 7850 03-Apr-2013
Hi Everyone!

My controller:

[HttpPost]
public ActionResult AddUsers(int projectId, int[] useraccountIds)
{
    ...
}
I'd like to post the parameters to the controller via AJAX. Passing the int projectId isn't a problem, but I can't manage to post the int[].

My JS code:

function sendForm(projectId, target) {
    $.ajax({
        traditional: true,
        url: target,
        type: "POST",
        data: { projectId: projectId, useraccountIds: new Array(1, 2, 3) },
        success: ajaxOnSuccess,
        error: function (jqXHR, exception) {
            alert('Error message.');
        }
    });
}
I tried it with a test array but no success. :( I also tried to set traditional: true, or contentType: 'application/json; charset=utf-8' but no success as 

well ...

The int[] useraccountIds posted to my controller is always null.

Updated on 03-Apr-2013

Can you answer this question?


Answer

1 Answers

Liked By