0

how can i send an object that have an array inside with ajax to a httppost method in mvc? im using @Html.AntiForgeryToken() in my view so i need to use ajax. here is my method

[HttpPost]
[ValidateAntiForgeryToken()]
public RedirectResult MultipleChangeSignupStatus(int[] id, string status)
{
   some code here
}

and the ajax i am using with a Link Button is like this:

var ids = [1 ,2, 3 ,4 ,5];
var token = $(':input:hidden[name*="RequestVerificationToken"]');
var data = {};
data['id'] = ids;
data[token.attr('name')] = token.val();   
data['status'] = 'accept';

$.ajax({
          url: '@Url.Action("MultipleChangeSignupStatus" , "Administrator")',
          data: data,
          type: 'POST',
           success: function () {

                }
       });

but in controller method i get a null value for id. status parameter is correctly set to 'accept' though id is still null

2 Answers 2

1

Try to construct your data object like this:

var ids = [1, 2, 3, 4, 5];

var token = $(':input:hidden[name*="RequestVerificationToken"]');
var data = { };
data['status'] = 'accept';
data[token.attr('name')] = token.val();

for (var i = 0; i < ids.length; i++) {
    data['id[' + i + ']'] = ids[i];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add the parameter traditional: true

$.ajax({
  url: '@Url.Action("MultipleChangeSignupStatus" , "Administrator")',
  data: data,
  type: 'POST',
  traditional: true,
  success: function () {
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.