0

I want to pass array data from my View to my Controller. However, when the View passes the data to the Controller, they are no rows passed.

What is wrnog with the below example? How can I pass an array to my Controller?

View:

      $.ajax({
      type: 'POST',
      url: urlString,
      data: {ids: testData},
      datatype: "json",
      traditional: true,
      success: function () {
         alert("test");
         },
         error: function (jqXHR, textStatus, errorThrown) {
           alert("jqXHR:" + jqXHR.status + " errorThrown: " + errorThrown);
         }
      });

Controller:

[HttpPost]
[ActionName("DefaultAction")]
public void SaveExceptions(int[] ids)   //ids has no data
{
  //do stuff
}

2 Answers 2

1

Sorry - I've just checked how I've done this before. It works for me like your original example, but quoting the the key in the data line like so:

data: {"ids": testData},

(Note the quotes around ids). Try that and see how it goes.

Sign up to request clarification or add additional context in comments.

Comments

0

In your javascript code, try renaming the variable "testData" to "ids" and changing the data line in the ajax config to:

data: ids,

1 Comment

I tried it, the controller method still reports zero rows. But the View does have rows in the ids[] var.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.