Tsukasa Tsukasa - 5 months ago 43
C# Question

Pass form as json to controller and parse

I have dynamically generated forms so I need to handle an unknown amount of inputs being passed to the controller. There can be no predefined model (I assume) to handle this so I figure sending a json string might be best.

The problem is parsing the json in the controller to JObject.

Here is what I'm using before submitting with an ajax request.

var json = JSON.stringify($(this).serialize());

now the string passed to the controller looks as follows


In the controller

JObject jo = JObject.Parse(json); //error

foreach (var o in jo)
string key = o.Key;
JToken val = o.Value;

The error i'm getting

"Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path ''


Couple of better options:

The Ajax data: payload is then just your $(this).serialize()