How to create a C # object from FormCollection with complex keys

I have a javascript object, obj , which is passed to the mvc action via $ .post () as follows:

 var obj = { Items: [{ Text: "", Value: { Property1: "", Property2: "" }, { Text: "", Value: { Property1: "", Property2: "" }] }; $.post('MyAction', obj, function() {}); 

the action signature is as follows:

 public ActionResult MyAction(FormCollection collection) { } 

I need to build an object from FormCollection , however I ran into a problem when the keys are in the form:

 "Items[0][Text]" "Items[0][Value][Property1]" "Items[0][Value][Property2]" "Items[1][Text]" "Items[1][Value][Property1]" "Items[1][Value][Property2]" 

I am wondering if there is a clean way to create the desired C # object from the given FormCollection . I understand that I can change the signature of the action method to take the type of object that interests me, but this presented its own problems.

+2
source share
1 answer

If you can change the javascript side so that you send the json version of the data instead, I would think that you can either accept the string, or deserialize json yourself in the controller, or make model binding for you.

+1
source

All Articles