0

Trying to post an array of strings to controller

My controller

public ActionResult GetMessage(List<string> ancestry)
{
}

My javascript

var s = ["a", "b", "c"];

$.post(newareaurl, { s: JSON.stringify(s) }, function (data) {

});

But the controller receives the data as a List with single element containing "[\"a\",\"b\",\"c\"]", I'm expecting it could receive a List with three elements.

I have tried setting the traditional style param serialization using traditional: true but it gives me "[object Object]" value in the controller

$.ajax({
  url: newareaurl,
  type: 'POST',
  data: { s: s },
  traditional: true,
  success: function (newTerritory) {
       console.log(newTerritory);
  },
  error: function () {

  }
  });

How to properly post the array?

strike_noir
  • 4,080
  • 11
  • 57
  • 100
  • try this SO question http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form – Colin Bacon Jul 01 '13 at 11:58
  • hi thanks for reply, my problem turns out that I didn't check the typeof the javascript var I was processing with. After I convert the object to array, it worked *of course still using traditional set to true – strike_noir Jul 01 '13 at 12:46

1 Answers1

0

found the method to convert object to array

var nArray = jQuery.makeArray(s);

worked

strike_noir
  • 4,080
  • 11
  • 57
  • 100