8

I've got an array of strings in JS. All the members are actually numbers.
My controller has an int[] parameter.

I send it from jquery:

$.ajax({url: someUrl, data: {ids : JSON.stringify(id_array) }, ...)

and I receive it using

public ActionResult MyAction(int[] ids) { ...

The parameter doesn't get filled, I've checked, the Request.Form["ids"] contains "[\"25\",\"26\"]", the string represention of the JSON array. Is there any way to do this automatically without a lot of int parsing?

0

4 Answers 4

11

Have you tried not stringifying the array and letting mvc's default model binding do the magic

$.ajax({url: someUrl, data: {ids : id_array }, ...)

I'm pretty sure .net MVC will see the array and see it is an array of ints and map it to your array of ints correctly

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

4 Comments

yes, Request.Form["ids[]"] will have the value "25,26", int[] still not binding.
@TDaver you need to set .ajax "traditional" property to true for shallow array serialization to work correctly according to api.jquery.com/jQuery.param as well
all I was missing was the traditional flag. Thanks
@Keith.Abramo please include a reference to the traditional property in your answer.
11

For MVC3 you need to configure jQuery to use traditional, "shallow" serialisation.. See here.

Client-side AJAX request:

jQuery.ajaxSettings.traditional = true; //enable "shallow" serialisation globally
$.get("someUrl", {ids : id_array });

Action Method:

public ActionResult MyAction(string[] ids) { ... }

Comments

4

You can do this in two different ways

1. Use $.ajax to post data to your action

$.ajax({
    url: '/myController/myAction',
    dataType: 'json',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    traditional: true,
    data: $.toJSON(json),
    success: function (data, textStatus, jqXHR) {
    ...
    }
});

Your action should look like this one

public class myController
{
    [HttpPost]
    public ActionResult myAction(List<int> json)
    {
        ....
    }
}

2. Use $.post to post your data

$.post(
    '/myController/myAction',
    { json: $.toJSON(products) },
    function (data) {
    ...
});

In your action you should deserialize JSON string

public class myController
    {
    public ActionResult myAction(string json)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        List<int> list = serializer.Deserialize<List<int>>(json);
    }
}

You need to download and include jquery.json-2.2.min.js to your code.

http://code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.min.js

Comments

0

You need to use

$.ajax({
    url: someUrl, 
    data: id_array.map( function(item){ return {name:'ids',value:item}; })
})

That is because in order to receive a list on the backend, the data need to be send in the form ids=3&ids=5&ids=1&ids=7


For IE versions that do not support .map use

function arrayToParamObject(name,array){
    var obj = [];
    for (var i = 0, len = array.length; i< len; i++){
        obj.push( {name: name, value: array[i] } );
    }
    return obj;
}

and use it

$.ajax({
    url: someUrl, 
    data: arrayToParamObject('ids', id_array)
})

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.