I'm just getting started with complex types in Web API. I created a simple class that the 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 up a Web API method that takes a GridColumnArgs:
public void PutGridLevel(int gridLevelID, GridColumnArgs columnArgs)
{
// my breakpoint is set here
}
And in JavaScript, made a 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 the breakpoint, my properties set on the JavaScript object literal are there, great.
However I need to pass in a collection of these GridColumnArgs objects. So if I change my Web API method thusly:
public void PutGridLevel(int gridLevelID, GridColumnArgs[] columnArgs)
{
// my breakpoint is set here
}
And adjust my testing JavaScript like 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 does not work. Routing works - I hit my breakpoint. But the parameter is an empty array.
So obviously the default formatters are not able to work with an array of a complex type as easily as they are with a single complex type.
I've read some answers and articles that reference writing a custom media formatter to solve this problem, but that seems like a lot of trouble to go to to accept an array of objects that the default media formatters already know how to work with... What is the quickest way to get this working?
Thanks very much for your help!