Featured post
c# - Server action never invoked - JSON deserialization failure? -
i have simple action in controller:
[httppost] public jsonresult action(menuset menuset) { //... }
where menuset
is:
public class menuset { private ienumerable<menuentry> _menuentries; public ienumerable<menuentry> menuentries { { return _menuentries; } set { _menuentries = value; } } } public class menuentry { private string _parentpagename; private ienumerable<string> _orderedpages; public string parentpagename { { return _parentpagename; } set { _parentpagename = value; } } public ienumerable<string> orderedpages { { return _orderedpages; } set { _orderedpages = value; } } }
from client side, i'm invoking action in way below:
$.post("controller/action", $.param({ menuentries: preparedata() }, true), null, "json");
where preparedata()
function returns menuentries
collection:
function preparedata() { var menuentries = new array(); var menuentry = { parentpagename: null, orderedpages: getpagesorder() } menuentries.push(menuentry); return menuentries; } function getpagesorder() { var values = new array(); values.push('samplepagename') return values; }
but json object appears not deserialized model @ server side - controller action never invoked. how make work ?
jarek,
it works fine if use $ajax, rather $post. i've added button index.aspx page so:
<input type="button" id="btngo" value="go" />
also, add new javascript file , paste following (save /scripts/tojson.js):
//source: http://www.overset.com/2008/04/11/mark-gibsons-json-jquery-updated/ (function($) { m = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' }, $.tojson = function(value, whitelist) { var a, // array holding partial texts. i, // loop counter. k, // member key. l, // length. r = /["\\\x00-\x1f\x7f-\x9f]/g, v; // member value. switch (typeof value) { case 'string': return r.test(value) ? '"' + value.replace(r, function(a) { var c = m[a]; if (c) { return c; } c = a.charcodeat(); return '\\u00' + math.floor(c / 16).tostring(16) + (c % 16).tostring(16); }) + '"' : '"' + value + '"'; case 'number': return isfinite(value) ? string(value) : 'null'; case 'boolean': case 'null': return string(value); case 'object': if (!value) { return 'null'; } if (typeof value.tojson === 'function') { return $.tojson(value.tojson()); } = []; if (typeof value.length === 'number' && !(value.propertyisenumerable('length'))) { l = value.length; (i = 0; < l; += 1) { a.push($.tojson(value[i], whitelist) || 'null'); } return '[' + a.join(',') + ']'; } if (whitelist) { l = whitelist.length; (i = 0; < l; += 1) { k = whitelist[i]; if (typeof k === 'string') { v = $.tojson(value[k], whitelist); if (v) { a.push($.tojson(k) + ':' + v); } } } } else { (k in value) { if (typeof k === 'string') { v = $.tojson(value[k], whitelist); if (v) { a.push($.tojson(k) + ':' + v); } } } } return '{' + a.join(',') + '}'; } }; })(jquery);
reference new file in index.aspx
<script src= "/scripts/tojson.js" type="text/javascript"></script>
and replaced $post javascript with:
$(document).ready(function() { $('#btngo').click(function() { $.ajax({ type: "post", url: '<%=url.content("~/home/action") %>', datatype: "json", data: { menuentries: preparedata() }, success: function(msg) { alert(msg); }, error: function(xhr) { alert(xhr.statustext); } }); }); }); function preparedata() { var menuentries = new array(); var menuentry = { parentpagename: "mypagename", orderedpages: getpagesorder() } menuentries.push(menuentry); // serialises javascript array correctly return $.tojson(menuentries); }
try changing action code below , try playing (this gives 2 options examine posted data):
[httppost] public jsonresult action(formcollection formcollection) { namevaluecollection test = httpcontext.request.form; // return json(test[0]); return json(formcollection[0]); }
it correctly deserialises inside 'action' action in controller.
give try. i'm sure it's 'fixed'
[edit] - halfaway fixed... you'll need work on how play object. i.e. either via formcollection or via test varibale. both give object in different guises.!!
- Get link
- X
- Other Apps
Comments
Post a Comment