Reputation: 14934
I've this Book class:
public class Book
{
public string Name { get; set; }
}
This Book ApiController with a post-method using a Complex type parameter:
[RoutePrefix("api/book")]
public class BookController : ApiController
{
[Route("create")]
public Book CreateBook([FromBody]Book book)
{
// Persist the book
return book;
}
}
I'm then using this jQuery code to test my Post-method:
var data = {"name":"test"};
$.ajax({
type: "POST",
url: "api/book/create",
data: JSON.stringify(data),
dataType: "json",
success: function (result) {
alert(result);
}
});
When running the jQuery, a breakpoint inside my CreaetBook action is trigged, but the Book.Name property is null
. I expect it to be test
.
Why is it always null?
Upvotes: 0
Views: 481
Reputation: 1444
Try without JSON.stringify
var data = {"name":"test"};
$.ajax({
type: "POST",
url: "api/book/create",
data: data,
dataType: "json",
success: function (result) {
alert(result);
}
});
Upvotes: 2