ApiController doesn't receive my DateTime?

My ApiController doesn't receive my DateTime object wrapped in a view model.

Here's my controller's contents.

public class FlowApiController : ApiController
{
    // GET api/flowapi
    public async Task<IEnumerable<FlowItemViewModel>> PostNewItems(SinceNullableViewModel input)
    {
        var since = input.Since; //since will be null for some reason

        ...

        return something;
    }
}

And then here's my jQuery AJAX call. I tried a number of different things, with no luck. This is what I have currently.

$.ajax("/api/FlowApi/NewItems", {
    method: "POST",
    data: JSON.stringify({
        Since: new Date()
    })
});

Finally, here's my SinceNullableViewModel.

public class SinceNullableViewModel
{
    public DateTime Since { get; set; }
}

What am I doing wrong here?

Chrome sends the data as follows:

enter image description here

Upvotes: 2

Views: 499

Answers (1)

cuongle
cuongle

Reputation: 75306

You are very close, just add one more header contentType to tell server that you are sending in JSON format:

$.ajax("/api/FlowApi/NewItems", {
    method: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({
        Since: new Date()
    })
});

Upvotes: 4

Related Questions