0

I'm at my wit's end here. I think I just need another set of eyes.

Method Signature:

public async Task<IHttpActionResult> Post(ApiRequest request)

Model:

[SuppressMessage("ReSharper", "CollectionNeverUpdated.Global")]
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global")]
public class ApiRequest
{
    [JsonProperty("allowLoadingToDataWarehouse")]
    public bool AllowLoadingToDataWarehouse { get; set; } 

    [JsonProperty("initialDelay")]
    public string InitialDelay { get; set; } 

    [JsonProperty("reportIds")]
    public IEnumerable<string> ReportIds { get; set; }

    [JsonProperty("reportTypeDelay")]
    public string ReportTypeDelay { get; set; } 

    [JsonProperty("runType")]
    [JsonConverter(typeof(StringEnumConverter))]
    public ReportRunType RunType { get; set; } 

    [JsonProperty("userId")]
    public string UserId { get; set; } 

    [JsonProperty("wwDelay")]
    public string WWDelay { get; set; }

    [JsonProperty("weeks")]
    public IEnumerable<string> Weeks { get; set; }
}

Javascript:

   var submitReportRequest = {
        userId: userid,
        reportIds: reportids,
        runType: 'Custom',
        weeks: selectedweeks,
        initialDelay: $('#InitialDelay').val(),
        reportTypeDelay: $('#ReportTypeDelay').val(),
        wwDelay: $('#WWDelay').val(),
        allowLoadingToDataWarehouse: $('#AllowLoadingToDataWarehouse').val()
   };

   $.post("/api/SubmitReport", JSON.stringify(submitReportRequest), function (data) {
        alert('success');
   });

Serialized Json From JavaScript Post:

{
  "userId": "30",
  "reportIds": [
    "59",
    "60",
    "61",
    "62",
    "63",
    "64"
  ],
  "runType": "Custom",
  "weeks": [
    "201409",
    "201410",
    "201411",
    "201412"
  ],
  "initialDelay": "00:00:00",
  "reportTypeDelay": "00:00:00",
  "wwDelay": "00:00:00"
}

Quickwatch of Deserialized Object Quickwatch of Deserialized Object

Initially I had int and TimeSpan for the Ids and Delays, respectively, and those were not deserializing correctly. So I changed them all to strings, and they're still not deserializing correctly.

What am I doing wrong or missing?

Edit: After trying every combination of attributes, I finally decided to stick it into the Fiddler Composer. And it works. So something must be off with my JavaScript.

5
  • What do you mean by "not deserializing correctly?" What behavior are you observing? Commented Mar 30, 2016 at 16:51
  • Try to remove all custom JsonProperty attributes, and eventually try to use [FromBody] ApiRequest request in the API signature. Just to understand what is going on... Commented Mar 30, 2016 at 16:54
  • Also, while I cannot say for sure it's causing your problem, there's no reason ReportIds can't be an ICollection or IList since you're deserializing it off of a Javascript array anyway, so there's no lazy iteration going on. Commented Mar 30, 2016 at 17:03
  • @NickBailey I originally tried a List but that wasn't working either. I'll try some other Collections. Commented Mar 30, 2016 at 17:22
  • @KennethK. None of my values are being deserialized. I'm getting nulls or default values. See the QuickWatch of Deserialized Object image. Commented Mar 30, 2016 at 17:22

1 Answer 1

2

Turns out that shorthand JQuery post() method was setting the Content-Type attribute on the Request to application/x-www-form-urlencoded; charset=UTF-8 when it needed to be set to application/json; charset=UTF-8

I found by watching the Network traffic in Chrome, and by changing my javascript to this answer.

Sign up to request clarification or add additional context in comments.

Comments

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.