0

Possible Duplicate:
Post an Array of Objects via JSON to ASP.Net MVC3

I'm trying to post a JSON object to an MVC3 controller. I seen solutions for this in stackoverflow, but most of the solutions are where the JSON object is then binded to a model. How do I make so that the post JSON object (array) is convert to an array of generic c# Object types? Or something that allows me to loop through the json objects. Please keep in mind the object is never the same. I basically want to create one controller that will allow me to loop the json object and the get the data from there. Currently right now I get an array of objects in the MVC controller, but then I can't seem to see the data in them. Any ideas?

Don't know if I'm going down the right path.

Thank you, -Tesh

Javascript:

 var parm = JSON.stringify({ session_id: sessionId, data: data, metaData: metaData })

    var ret = "";
    $.ajax({
        type: 'post',
        cache: false,
        url: path + "/Common/Export/Load",
        data: parm,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        traditional: true,
        async: false,
        success: function (data) {
            //ret = data.Table[0].Column1;
        },
        error: function (event, request, settings) {
            //alert('save error: ' + JSON.stringify(request)) + ', ' + JSON.stringify(settings); 
        }
    });

MVC Controller

[HttpPost]
    public ActionResult Load(String session_id, List<object> data, Object metaData)
    {




        return View();
    }

JSON Object Passed to Controller. The object can at any time.

{

"Table": [ { "first_name": John, "last_name": Doe, "creation_date": "2012-10-04T15:50:37.55", "created_by": Mary Jane, "last_modified_date": "2012-10-04T15:50:37.55", "last_modified_by": Jon Mitchell

},
{
   "first_name": Bill,
  "last_name": Doe,
  "creation_date": "2012-10-04T15:50:37.55",
  "created_by": Jane Wick,
  "last_modified_date": "2012-10-04T15:50:37.55",
  "last_modified_by": Jon Mitchell
}

] }

Community
  • 1
  • 1
MindGame
  • 1,211
  • 6
  • 29
  • 50
  • 1
    You are using `stringify` so remove `dataType:'json'` and `contentType` fields from your AJAX call... and it should work fine :) dataType is the type of data returned from the server.. Not the data to be sent to the server – AdityaParab Oct 15 '12 at 02:54
  • After I removed dataType and contentType fields I no longer get an array and the value passed to controller is null. – MindGame Oct 15 '12 at 15:38

1 Answers1

1

Object by default has very few properties anyway, so the JSON binder wont bind anything to them.

If you have common properties that you want to loop over and get, declare those as a ViewModel and then set that as a parameter in your list object.

public class BaseModel
{
     public DateTime creation_date {get;set;}
}

You can then use

public ActionResult Load(String session_id, List<BaseModel> data, Object metaData)
{     
     ...
}

Should you really need to bind it to an object, you can try something like in this link:

Turning JSON into a ExpandoObject

HTH

Si

Slicksim
  • 7,054
  • 28
  • 32