Search code examples
.net-coreasp.net-core-mvcunobtrusive-ajax

.Net Core 3.0 AJAX POST Body Always Null


I've recently converted my project from .Net Framework 4.7 to .Net Core 3.0. I'm having trouble getting my AJAX post to work.

Here is what's working in .Net Framework 4.7:

View:

@using (Ajax.BeginForm("Save", "Controller", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "OnSaveSuccess", OnFailure = "OnFailure" }, new { Model }))
{
  ... Model Code Here
}

Controller:

[HttpPost]
public JsonResult Save(Contract contract)

Here's what is not working in .Net Core 3.0:

View:

<form method="post" action="/Controller/Save" data-ajax="true" data-ajax-method="post" data-ajax-sucess="OnSaveSuccess"  data-ajax-failure="OnFailure">

Controller:

[HttpPost]
public JsonResult Save([FromBody] Contract contract)

The Contract object comes as NULL in this request. Is there something I'm doing wrong?

Thanks


Solution

  • It ended up being my fault. I had put:

    $.ajaxSetup({
       contentType: "application/json"
    });
    

    This was causing the POST to not map properly. Once I removed it the Model came over correctly.