I'm having a hard time to find a solution for this problem. My API controller is not able to get the content of the array that I posted and is always return 0 element.
Model class:
public class Author
{
public int id { get; set; }
public int referenceId{ get; set; }
[Newtonsoft.Json.JsonIgnoreAttribute]
public virtual ICollection<AuthorBook> AuthorBooks { get; set; }
}
public class AuthorBook
{
public int authorId { get; set; }
public int bookId { get; set; }
}
API controller method:
[Route("api/authorBooks")]
[HttpPost]
public IList getAuthorBooks(Author author)
{
return author.AuthorBooks.ToList();
}
JavaScript client:
var auhtorItem =
{
"id": 8,
"referenceId" : 1 ,
"AuthorBooks": [{ authorId: 9, bookId : 23 }, { authorId: 9 , bookId : 25}]
};
return $.ajax({
url: booksUrl + "authorBooks",
type: 'POST',
data: JSON.stringify(auhtorItem ),
dataType: 'json',
contentType: 'application/json',
crossDomain: true,
cache: false
});
I'm able to get the id and the referenceId values but the AuthorBooks is empty and the count is 0 instead of 2.
Any idea why?
Newtonsoft.Json.JsonIgnoreAttributeis present onAuthorBooks?this is not the problem. Newtonsoft.Json.JsonIgnoreAttribute should not be present on AuthorBooks- then why is it present in the Code Snippet?