Thursday, 15 March 2012

javascript - How to read serialized data (sent through Jquery POST) in MVC collection -



javascript - How to read serialized data (sent through Jquery POST) in MVC collection -

i have form , 1 submit button click, serialize form , send mvc action.

jquery code

var formcollection = $('form').serialize(); var path = $(this).attr('data-content-url'); // check if no more error $.ajax({ type: 'post', url: path, cache: false, data: { collection: formcollection }, datatype: 'json', success: function (data) { // stuff } });

mvc side

[httppost] public actionresult methodname( formcollection collection ) { }

i serialize info in collection variable

name=test&id=1 collection[0]

how can break collection[0] name , id can assigned straight class parameter,

person p = new person { collection["name"], collection["id"] }

many thanks

this worked, create new helper class convert serialized info in formcollection expect

private formcollection deserialize(formcollection form) { formcollection collection = new formcollection(); //un-encode, , add together spaces in string querystring = uri.unescapedatastring(form[0]).replace("+", " "); var split = querystring.split(new [] {'&'}, stringsplitoptions.removeemptyentries); dictionary<string, string> items = new dictionary<string, string>(); foreach (string s in split) { string text = s.substring(0, s.indexof("=")); string value = s.substring(s.indexof("=")+1); if (items.keys.contains(text)) items[text] = items[text] + "," + value; else items.add(text, value); } foreach (var in items) { collection.add(i.key, i.value); } homecoming collection; }

javascript model-view-controller collections

No comments:

Post a Comment