0

I am POSTing to the server (.Net) and I am having trouble passing arrays to the controller action. I've tried just about every possible combination without any luck. However, one of them is puzzling me.

If I perform this request :

var dataArray = [ { /* some plain object */ }, { /* another plain object */ ], ... ];

$.ajax(url, {
   type: "post",
   data: { models: dataArray }
});

Resulting a request data sent like

models[0][property1]:value1
models[0][property2]:value2
...
models[1][property1]:value3
models[0][property2]:value4
...

Unfortunately, the request is just not understood by .Net MVC4. Following a related SO question I've tried traditional: truebut the request sent to the server looks like this:

models:[object Object]

Which, obviously, sends not an object, but the string "[object Object]". ...What's wrong with this? Am I doomed to send serialized strings (and have to manually deserialize them on the server side) for every request involving non primitive parameters?

Note : this is my action method. As for now, everything I try results in

  1. the parameter is an array of the correct size, but each item is a new unmodified (empty) object or
  2. the parameter is null

    [HttpPost]
    public ActionResult UpdateModels(Models.SimpleModel[] models)
    
Community
  • 1
  • 1
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214

2 Answers2

2

Try using JSON.stringify for array like JSON.stringify(dataArray)

UPDATE

MVC3 converts the string data to the .NET object automatically, if we use stringify while using ajax calls. I think same should be the case with MVC4.

Update 2
set contentType attribute to application/json; charset=utf-8

Community
  • 1
  • 1
PranitG
  • 121
  • 8
  • I tried, it doesn't work unless I deserialize the string manually (so instead of having an `IEnumerable<>` or an array of my objects, I need to receive a string). I'd like to find how .Net MVC4 can accept what I'm sending without being such a jerk – Yanick Rochon Apr 03 '13 at 17:36
  • Yes, however MVC4 does not understand JSON data if the contentType is not explicitly set. – Yanick Rochon Apr 03 '13 at 17:49
2

Sample JQuery

var MyConnectionList = {
    ColorList: []
};

function SendStream() {
    MyConnectionList.ColorList.push({
        "Name": 'Test1',
        "Color": 'red'
    });

    MyConnectionList.ColorList.push({
        "Name": 'Test2',
        "Color": 'Green'
    });

    $.ajax({
        url: url,
        data: JSON.stringify(MyConnectionList),
        async: true,
        type: 'POST',
        beforeSend: function (xhr, opts) {
        },
        contentType: 'application/json; charset=utf-8',
        complete: function () {  },
        success: function (data) {
        }
    });
}

Sample Controller

public ActionResult SendStream(List<Sample> ColorList)
{
    return null;
}
  • Nice! `JSON.stringify(object)` with `contentType: 'application/json; charset=utf-8'` solved the problem! I can now receive my arguments just fine! Thanks! – Yanick Rochon Apr 03 '13 at 17:43
  • This no longer works. The default contentType is application/x-www-form-urlencoded; charset=UTF-8, and when changed to the normal contentType used since forever, the Payload becomes the entire array as a single parameter name follow by empty space as parameter value (check under F12->Network). – Henrik Erlandsson Jun 14 '22 at 14:39