Upon receiving a model of type HistorySearch
in my view, I am looking to resend this model to the controller using ajax
:
$("#exportCsv").click(function () {
// get model as json
var searchData = '@Html.Raw(Json.Encode(@Model))';
searchData = JSON.stringify({ 'search': searchData });
$.ajax({
//contentType: 'application/json; charset=utf-8',
url: '@Url.Action("ExportToCsv", "BankCosts")',
type: 'POST',
data: searchData,
dataType: 'json',
error: function (xhr) {
alert('Error: ' + xhr.statusText);
},
async: true,
});
});
However, there was an issue with the contentType
that caused the passed model to become null in the controller.
In addition, here is how my controller is structured:
[HttpPost]
public void ExportToCsv(HistorySearch search)
{
// search properties are not filled. They are set to default value
}
It seems that the binding is not functioning correctly as the received search
properties are being set to default values. Can you identify the issue?