Reputation: 9709
I have a Jquery Ajax call more or like below
$.ajax(url, {
type: httpMethod,
contentType: "application/json; charset=utf-8",
data: '{name:"abc",age:23,address:"29 judd ce"}',
The web api action is
public HttpResponseMessage Post([FromBody] FormDataCollection data)
{
However the parameter 'data' is always null.
Is this a web api limitation or am i doing it the wrong way
Thanks
Upvotes: 1
Views: 2255
Reputation: 731
it's possible
$.ajax("http://localhost:57281/api/Values", {
type: "POST",
data: '{name:"abc",age:23,address:"29 judd ce"}',
contentType: "application/x-www-form-urlencoded"
});
//POST api/values
public void Post([FromBody]FormDataCollection value)
{
}
Also works without [FromBody]
But you will get only ONE pair of key/value
Better use something like that
$.ajax("http://localhost:57281/api/Values", {
type: "POST",
data: 'name=abc&age=23&address=29 judd ce',
contentType: "application/x-www-form-urlencoded"
});
& is used as a separator for each pair
Upvotes: 0
Reputation: 1039060
Use a view model instead:
public class UserViewModel
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
that your controller action will take:
public HttpResponseMessage Post(UserViewModel model)
{
...
}
Upvotes: 1