0

I am making an ajax call form the javascript to MVC controller, passing the array of object to controller action.

Js Code :

function Constructor(p1, p2) {
    this.foo = p1;
    this.bar = p2;
}

var Obejct_Array = new Array();

Obejct_Array[Obejct_Array.length] = new Constructor("A", "B");
Obejct_Array[Obejct_Array.length] = new Constructor("C", "D");

$.post("/_Controller/_Action", { ObjectArray : Obejct_Array });

C# code

public Class Example
{
  public string foo { get; set; }
  public string bar { get; set; }
  public string Prop3 { get; set; }
}

 //Action in Controller
 public void _Action(Example[] ObejctArray)
 {
  //Here the size of ObjectArray is 2 but the properties are all null. Whats the problem ?
 }

Both entries from javascript array is passing to the controller`s action method but the property values are showing null. Can anybody tell me the problem ?

3
  • Stringify it, and convert it back on the serverside! Commented Sep 1, 2012 at 16:58
  • @adeneo can you please give a an example code Commented Sep 1, 2012 at 17:00
  • 1
    Not sure how you do it in C#, but Lee Taylor has already posted a link below. In javascript you'd do JSON.stringify(Obejct_Array), and I'm guesing you'd want that as a string since it's an array and all, and just convert it back on the server. Commented Sep 1, 2012 at 17:09

2 Answers 2

1

You'll have to use JSON to do this. Depending on actual code of your Constructor class, this will give you something like this on Javascript side:

[{"foo":"A", "bar":"B"}, {"foo":"C", "bar":"D"}]

which is the JSON representation of an array of 2 objects with properties foo and bar.

On the server side, you'll have to convert the JSON structure back to an actual object structure. Surely there are libraries for that (I'm no C# guy, so I don't know any)

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

1 Comment

The library in C# to use is JSON.net (james.newtonking.com/pages/json-net.aspx)
0

If you are using MVC, you can pass your array data such that it will bind directly to the array parameter in your action method. MVC will expect the data as follows:

// in js
var data = {
    // could also put the name of the parameter before each [0/1] index
    "[0].foo": "A",
    "[0].bar": "B",
    "[1].foo": "C",
    "[1].bar": "D"
};

// a js function to put the data in this format:
function (array, prefix) {
    var prefixToUse = prefix || "",
        data = {},
        i, key;
    for (i = 0; i < array.length; i++) {
        for (key in array[i]) {
            // might want to do some filtering here depending on what properties your objects have
            data[prefixToUse + "[" + i + "]." + key] = array[i][key];
        }
    }

    return data;
}

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.