I am having a strange problem with ASP.NET MVC and sending an AJAX post. I have an anchor tag on the page with id 'lnkGetExpirDates'. When the user clicks it I am trying to simply send this hardcoded data to my controller so it can use it as params and return a result back to the page.
However, in the controller the params are always null/default values. Please see my code below and let me know if I am making a silly mistake or something.
Here is my AJAX post:
$("#lnkGetExpirDates").click(function () {
e.preventDefault();
var data = {
StartDate: "1/19/2016",
EndDate: "4/19/2016",
ProductType: "New",
Count: 1
};
$.ajax({
url: '/Products/GetExpirationDates',
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
dataType: 'html',
success: function (data) {
$('#ExpirationDates').val(data);
}
});
});
Here is my controller's action method:
// POST: Product/GetExpirationDates
[HttpPost]
//[ValidateAntiForgeryToken]
public IActionResult GetExpirationDates(GetExpirationDatesViewModel vm)
{
// TODO: Get expiration dates and return them in the response
return View();
}
Here is the GetExpirationDatesViewModel:
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string ProductType { get; set; }
public int Count { get; set; }
Please note that I have even tried this without using a model at all and simply just adding parameters to the Action Method such as startDate, endDate, productType, and Count and still everything comes in as null.