0

Have a ASP.Net Core full framework WebApi controller with the following controller method.

[HttpPost("{id}/json")]
public async Task<ILibraryModel> PostFromBody(int id, [FromBody]LibraryFilterOrder stuff, int pageSize, int pageNumber = 1)
{
    return await DoStuff(id, stuff, pageSize, pageNumber);
}

It is taking in a model of LibraryFilterOrder with the following definition.

public class LibraryFilterOrder
{
    public List<ContainerView.ContainerFilterType> Filters { get; set; }
    public List<ContainerView.ContainerOrderType> Orders { get; set; }
}

public class ContainerFilterType
{
   public string ColumnPrefix { get; set; }
   public string ColumnName { get; set; }
   public string FilterValue { get; set; }
}

public class ContainerOrderType
{
   public string ColumnPrefix { get; set; }
   public string ColumnName { get; set; }    
   public bool SortAscending { get; set; }
   public int SequenceId { get; set; }
}

Am using the Javascript Fetch API using Typescript

fetch(`/api/Library/${pageIndex}/json?pageSize=${pageSize}&pageNumber=${pageNumber}`, {
                    method: "POST",
                    headers: {                        
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    body: JSON.stringify({ stuff: { filters: filters, orders : orders } })
                })
                .then(response => response.json() as Promise<LibraryModel>)
                .then(data => {
                   //DO stuff
                });

The Chrome Dev tools provides the following : enter image description here With all of that the stuff variable in the controller is coming through with 2 properties of null.

Any help with this would be much appreciated, have been pulling my hair out for the last day or so trying to work it out, have taken the [FromBody] tag off of the parameter, and that still comes through as null.

1 Answer 1

2

You pass incorrect JSON.

Change this line:

body: JSON.stringify({ stuff: { filters: filters, orders : orders } })

to this one:

body: JSON.stringify( { filters: filters, orders : orders } )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Roma, works like a charm, will accept it, when get to the 5 minute mark, stupid things like that...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.