asp.net web api - Passing an array of complex type to a Web API method -
i'm getting started complex types in web api. created simple class default formatter should have no problem working with:
public class gridcolumnargs { public gridcolumnargs() { } public int fieldid { get; set; } public bool visible { get; set; } }
and set web api method takes gridcolumnargs:
public void putgridlevel(int gridlevelid, gridcolumnargs columnargs) { // breakpoint set here }
and in javascript, made simple testing method:
var columnargs = { fieldid: 1, visible: true }; myhelperlibrary.put({ url: 'api/grid?gridlevelid=123', obj: columnargs, done: function () { alert('ok!') }, fail: function () { alert('no good!') } });
works perfectly. hits breakpoint, properties set on javascript object literal there, great.
however need pass in collection of these gridcolumnargs objects. if change web api method thusly:
public void putgridlevel(int gridlevelid, gridcolumnargs[] columnargs) { // breakpoint set here }
and adjust testing javascript this:
var columnargs = [ { fieldid: 1, visible: true }, { fieldid: 2, visible: false } ]; myhelperlibrary.put({ url: 'api/grid?gridlevelid=123', obj: columnargs, done: function () { alert('ok!') }, fail: function () { alert('no good!') } });
the call not work. routing works - hit breakpoint. parameter empty array.
so default formatters not able work array of complex type single complex type.
i've read answers , articles reference writing custom media formatter solve problem, seems lot of trouble go to accept array of objects default media formatters know how work with... quickest way working?
thanks help!
not sure if gonna help, try adding attributes.
[jsonproperty("fieldid")] public int fieldid { get; set; }
or change
public void putgridlevel(int gridlevelid, dynamic columnargs)
to eliminate possible type issues. might 'var' instead of dynamic type or try 'object' can't recal , can't check away vs atm.
Comments
Post a Comment