AJAX Post to ASP.NET MVC 6 Controller action method and parameters are null AJAX Post to ASP.NET MVC 6 Controller action method and parameters are null ajax ajax

AJAX Post to ASP.NET MVC 6 Controller action method and parameters are null


When you specify the content type value as application/json, the ajax request made to server will send your js object as the Request Payload, in the body of the request (in json format). So in your action method you should use the [FromBody] attribute.

public IActionResult GetExpirationDates([FromBody] GetExpirationDatesViewModel vm){   return View();}

You should do this when you want to send a complex js object with properties which is again another model/type

But the data you are sending is a flat view model. So you do not really need to json stringify the js object. Also you may remove the content type property as well as you are not going to send a stringified version of a complex js object.

This should work fine.

$.ajax({    url: '/Products/GetExpirationDates',    type: 'POST',    data: data,          dataType: 'html',    success: function (data) {        console(data);    }});

The above code will send the js object as key value pairs (normal form data) and the model binder will be able to map the form data to your view model object in the server. Take a look at this post for more detailed explanation.

Also i recommend you to use the Url.Action helper method to generate the proper relative url to the action method(s).

url: '@Url.Action("GetExpirationDates","Products")',

The above should work if your js code is inside a razor view. If your code is inside an external js file, Use the solution described in this post.