8

I am trying, and struggling, to send an array via JSON to a MVC controller action.

Here is what I have and what i've tried...

//Get checked records
var $checkedRecords = $(':checked'); //e.g. 3 rows selected = [input 4, input 5, input 6]

//Have tried following:
sendingVar: $checkedRecords.serializeArray(); // gives array of 0's
sendingVar: JSON.stringify($checkedRecords); // gives "{\"length\":1,\"prevObject\":{\"0\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"context\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"length\":1},\"context\":{\"jQuery1313717591466\":1,\"jQuery1313717591653\":13},\"selector\":\":checked\",\"0\":{}}"...wtf

//Post
$.post(url, { sendingVar: sendingVar }, function(data) {alert(data); });

How do I do it ?

edit: to those that suggest sending $checkedRecords "as is" from the top line - that is not working. I get a strange exception somewhere in the jquery framework :(

uncaught exception: [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://.../.../.../jquery-1.4.4.min.js :: <TOP_LEVEL> :: line 141" data: no]

which I think means it is attempting to assign null to something it cannot.

Edit: i'm using MVC2 not 3

Edit2: After @Monday's answer- the problem is due to how I have built the array like [input 4, input 5, input 6] and not [4,5,6] - any ideas how I can just get the values in the array instead ?

Edit3: Stop voting up duplicate when it's not. Did you actually read my problem or read the problems linked? this is a different issue

@Daveo:

I don't want to build in an overriding custom attribute just to send an array from JSON, that is rediculous as we've already covered in this question-it is not necessary.

MVC3 - irrelevant

Community
  • 1
  • 1
baron
  • 11,011
  • 20
  • 54
  • 88
  • 1
    possible duplicate of: http://stackoverflow.com/questions/320291/how-to-post-an-array-of-complex-objects-with-json-jquery-to-asp-net-mvc-controll and http://stackoverflow.com/questions/4789481/post-an-array-of-objects-via-json-to-asp-net-mvc3 – Daveo Aug 19 '11 at 01:50
  • What is your desired result? Your array is of jQuery objects, the stringify version looks right to me. What are you expecting it to look like? – BZink Aug 19 '11 at 02:04
  • it is just an array containing string ID's of the checked items. I want checkedRecords to contain an array of all ID's of items selected. The selected input's `value` contains the ID value I need. – baron Aug 19 '11 at 02:07

3 Answers3

24

Here is my demo,use mvc2,hope some helps~

The key to success is traditional

set the traditional parameter to true

$(function(){
    var a = [1, 2];
    $.ajax({
       type: "POST",
       url: "<%= ResolveUrl("~/Home/PostArray/") %>",
       data: {orderedIds: a},
       dataType: "json",
       traditional: true,
       success: function(msg){alert(msg)}
    });
})

Since jquery 1.4 this parameter exists because the mechanism to serialize objects into query parameters has changed.

and action is~

[HttpPost]
public ActionResult PostArray(int[] orderedIds)
{
    return Content(orderedIds.Length.ToString());
}
Monday
  • 1,403
  • 12
  • 10
  • Unfortunately, this doesn't work. `orderedIds` is null in the controller method. I suspect because ajax actually does a `GET`? – baron Aug 19 '11 at 02:26
  • 1
    @baron - In my location, it works fine. `orderedIds` is a parameter name – Monday Aug 19 '11 at 02:33
  • 1
    Hmmm apologies, you are correct this code does work. So the problem lies in how I am getting the array of checked Id's with `var $checkedRecords = $(':checked');` which gives me `[input 3, input 4, input 5]` where I actually need: `[1, 2]`. Thank you – baron Aug 19 '11 at 02:46
  • thanks, i got it with `var items = []; $("input:checked").each(function () { items.push($(this).val()); });` – baron Aug 22 '11 at 00:40
0

you can also use JSON.stringyfy to sent the data as a string then use JavaScriptSerializer class to retrive the data.

In C# code, to get the data, will look like this:

JavaScriptSerializer js = new JavaScriptSerializer();

js.Deserialize<T>(string paramiter);
Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
JoshSabb
  • 1
  • 3
0

use this simple code

var url = '/sampleController/sampleAction'
var params = {sendingVar: [1,2]}
$.post(url, params , function (data) {
   $("#lblResult").html(' : ' + data);
}).fail(function (e) {
   alert(e);
});
R.Akhlaghi
  • 729
  • 1
  • 12
  • 23