2

I have this controller:

public HomeController : Controller
{
   public ActionResult(MyObj[] myObjArr)
   {
   }
}
public class MyObj
{
  public Datetime d {get;set;}
  public int x{get;set;}
  public string yy {get;set;}
}

My javascript code is the following:

var obj = { d:new Date(), x : 10, yy : 'hello' };

$.ajax({
            type: "POST",
            url: server,
            dataType:"json",
            data: {obj,obj},
            success: function(data) {
                alert(data);
            }
        });

It simply dont work, i've tried:

$.JSON({obj,obj});
JSON.stringify({obj,obj});

On data but nothing. Every possible combination i've probably tried out but on server side myObjArr just keeps at null, i've tried numerous walkthroghts, i replaced [] to List, ICollection, etc Any suggestions? Thanks.

2
  • > data: {obj,obj} That's not valid JavaScript. Surely you see the SyntaxError in your console. Commented Apr 19, 2011 at 1:39
  • arr = []; arr.push(obj);arr.push(obj); you surely get the idea . . . Commented Apr 19, 2011 at 1:41

1 Answer 1

2

This seems fine for me.

public HomeController : Controller
{
   public ActionResult(MyObj[] myObjArr)
   {
   }
}
public class MyObj
{
  public Datetime d {get;set;}
  public int x{get;set;}
  public string yy {get;set;}
}

JS:

function makeParams(arr, namespace) {
    var tempObj = {};
    for (var i = 0; i < arr.length; i++) {
        var o = arr[i];
        for (var k in o) {
                tempObj[namespace + "[" + i + "]." + k] = o[k];
        }
    }
    return tempObj;
}

var arr = [{ d:new Date(), x : 10, yy : 'hello' }];
var namespace = "myObjArr";
var data = makeParams(arr, namespace); //eg. { 'myObjArr[0].x': 10 }
$.ajax({
            type: "POST",
            url: server,
            dataType:"json",
            data: data,
            success: function(data) {
                alert(data);
            }
        });
Sign up to request clarification or add additional context in comments.

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.