I’m sure I’ve done this in another solution, but I can’t seem to find any solution as to do it again and wondered if anyone can help me…
This is my WebAPI code:
public class WebController : ApiController
{
public void Get(string telephone, string postcode, List<Client> clients)
{
}
}
And, calling this from jQuery:
function Client(name, age) {
this.Name = name;
this.Age = age;
}
var Clients = [];
Clients.push(new Client("Chris", 27));
$.ajax({
url: "/api/Web/",
data: { telephone: "999", postcode: "xxx xxx", clients: Clients }
});
But the “clients” object always comes back as null. I’ve also tried JSON.stringify(Clients), and this is the same result. Can anyone see anything obvious I’m missing here?
The action parameter binding in Web API is different than in ASP.NET MVC (you can read more about it in this article):
If you don’t want to follow the conventions you need to mark your parameters with the
[FromBody]or[FromUri]attribute depending from where do you want to bind them.In you case because you are using a GET request you need to mark your
clientsparam with[FromUri]to bind correctly: