JQuery Array in Ajax post seems to serialise and appear as one element in controller list/array -
i have array in jquery holds id & value of attributes need pass mvc controller. array seems populate correctly in jquery , ajax post controller works too. however, value being passed controller appears flat single element in controller array (i.e. array values flattened 1 sequential string value).
here jquery:
var attlistarray = new array(); table.find('tr').each(function (i, el) { var tds = $(this).find('td'), attname = tds.eq(0).text(), attvalue = tds.eq(1).text(), attid = tds.eq(2).text(); attlistarray[i] = attid + '~' + attvalue; alert(attlistarray[i] + ' - ' + i); }); $.ajax({ type: "post", async: true, contenttype: "application/json;charset=utf-8", url: "@url.action("saveform", "home")", data: '{"parms":"' + formelements + '" , "attlist":"' + attlistarray + '"}' , datatype: "json", traditional: true, success: function (data) { alert("success"); }, error: function () { alert("an error has occurred"); } });
the controller action is:
public jsonresult saveform(string parms, list<string> attlist) { models.result result = new models.result(false, "none"); namevaluecollection qscoll = httputility.parsequerystring(parms); string s1 = ""; string s2 = ""; foreach (string s in qscoll.allkeys) { s1 = s1 + " , " + s; s2 = s2 + " , " + qscoll[s]; //console.writeline(" {0,-10} {1}", s, qscoll[s]); } try { result.success = true; } catch (exception ex) { result.success = false; result.error = ex.message; //log event log //drs_repository.logevent(ex.message, ex.source); } return json(result, jsonrequestbehavior.allowget); }
as example, have 2 strings in 2 array cells: [0] = '56~z', [1] = '59~x'. when array passed controller, list attlist contains 1 count of string = '56~z,59~x'. expecting array have count of 2 & elements passed list or array separate elements:
e.g. attlist[0]="56~z" , attlist[1]="59~x"
but can't result. i've tried passing string[] attlist, i've tried adding square & curly brackets around array in jquery , i've tried json.stringify array can't array pass list/array controller.
could explain i'm going wrong?
thanks in advance.
shuja
you can't concantenate strings , arrays, it's suprising if don't [object, object]
on server?
as attlistarray
array, should replacing line
data: '{"parms":"' + formelements + '" , "attlist":"' + attlistarray + '"}' ,
with
data: {parms : formelements, attlist: attlistarray},
as jquery accepts objects directly
Comments
Post a Comment