Hi @Sherpa,
I found you are using DataTable javascript library in your project, and I believe you can pass the data successfully by using basic ajax method(like the sample SurferOnWww shared).
And I following your sample code and make it work in my side. Please kindly check the data area under the DataTable/ajax
like below. Hope it can help you.
Reports/Index.cshtml
@model ReportDemo.Models.ReportsConfigurationVM
<div class="display-area">
<div style="padding-top: 10px; padding-left: 5px">
<form id="reportForm" method="post">
<div asp-validation-summary="All" class="text-danger" style="text-align:left"></div>
<div class="card" style="width: 18rem;height: auto;width:800px ">
<div class="card-header">
Available Reports
</div>
<div class="form-floating register-input">
<!-- input -->
<input id="Year" name="Year" placeholder="Enter Year" asp-for="Year" class="form-control" />
<!-- label -->
<label>Enter Year</label>
<!-- required -->
<span asp-validation-for="Year" class="text-danger"></span>
</div>
</div>
</form>
</div>
<div style="padding-top: 20px; padding-bottom:20px">
<button class="btn btn-lg btn-primary" onclick="javascript:runReport();">Run Report</button>
<button id="clear" class="btn btn-lg btn-secondary">Clear Filters</button>
</div>
</div>
<script>
function runReport() {
$('#riskReport').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false,
"responsive": true,
"ajax": {
url: "/Reports/ProcessReport",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: function(d) {
var reportDataArray = $('#reportForm').serializeArray();
var reportDataObj = {};
$.each(reportDataArray, function(index, field) {
reportDataObj[field.name] = field.value;
});
return JSON.stringify(reportDataObj);
},
"dataSrc": ''
},
"columns": [
{ "data": "customerNumber", "name": "CustomerNumber", "autoWidth": true },
{ "data": "customerStatus", "name": "CustomerStatus", "autoWidth": true },
{ "data": "startYear", "name": "StartYear", "autoWidth": true }
]
})
}
</script>
<table id="riskReport" class="display" style="width:100%">
<thead>
<tr>
<th>Customer Number</th>
<th>Customer Status</th>
<th>Start Year</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
ProcessReport method
[HttpPost]
public async Task<IActionResult> ProcessReport([FromBody] ReportsConfigurationVM reportsConfigurationVM)
{
if (reportsConfigurationVM == null)
{
return BadRequest("Error: Model binding failed. The model is null.");
}
List<RiskByYearTest> myList = new List<RiskByYearTest>
{
new RiskByYearTest { CustomerNumber = "4549601", CustomerStatus = "Active", StartYear = "2022" },
new RiskByYearTest { CustomerNumber = "4549602", CustomerStatus = "Closed", StartYear = "2023" }
};
return Ok(myList);
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Jason