0

I've created an api controller called VisitController:

public class VisitController : ApiController
{
    public class VisitAddModel
    {
        string VisitId { get; set; }
        DateTimeOffset VisitDate { get; set; }
        string NoteText { get; set; }
        string DoctorNote { get; set; }
    }
    [HttpPost]
    public bool Index([FromBody]VisitAddModel model)
    {
        return true;
    }
}

And I am attempting to POST data to it via jQuery and AJAX:

var newRec = {
    VisitId: 'xxxxxxxxxxxxxxxxxxxx',
    VisitDate: '3/27/2018',
    NoteText: 'Test note text',
    DoctorNote: 'Test doctor note',
    _pending: true
};
$.ajax({
    url: apiUrl + "/visit",
    type: "POST",
    data: newRec,
    success: function (response) {
        if (response && response.VisitId)
            previousVisitStore[response.VisitId]._pending = false;
    }
});

My controller is getting hit, but every time, no matter what, the model, or the values in the model, are null. I've seen plenty of posts about POST-ing data to an MVC controller via jQuery, and most talk about adding contentType: "application/json", or using JSON.stringify, or using/not-using [FromBody]. I have tried EVERY. SINGLE. COMBINATION. of those various configurations (a total of 9 different configuration patterns. And depending on the set of configuration properties, I either see a model with every value null in my api controller, or, I see a completely null model in my api controller. I've even checked the F12 tools to see if the values are there in the network traffic. And they are.

I must be overlooking something. What could I be missing here? What do I need to do to ensure my api controller deserializes these values correctly? I am using Asp.Net MVC 5.4. This is my routing, if that might have something to do with it:

    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47
  • 4
    All your properties in `VisitAddModel` are private right now. Try changing them to public and give it another go. – Arian Motamedi Mar 27 '18 at 20:12
  • Have you tried sending the stringified version of your object as data along with content type as "application/json" ? Also try using public settable properties on your view model. See this for some samples https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-an-object/20226220#20226220 – Shyju Mar 27 '18 at 20:12
  • 1
    Change data on your post to `data: JSON.stringify(newRec)` – ElasticCode Mar 27 '18 at 20:16
  • @PoweredByOrange: Ugh, that was it. I cannot believe I missed that. My properties needed to be `public`. That's why the model itself was not null, but the values were. Thanks! – Matt Spinks Mar 27 '18 at 20:18

0 Answers0