为什么通过POST(ajax)发送JSON数据会返回空参数?
大家下午好:) 我正在开发asp.NETMVC应用程序。 我使用jquerydatatable来显示我的数据。 实际上,我的ajax Post请求调用一个控制器方法,该方法必须接收一些参数。 问题是它们是空的。我还有一个Post 500错误:( 这是我的班级:为什么通过POST(ajax)发送JSON数据会返回空参数?,ajax,post,asp.net-ajax,Ajax,Post,Asp.net Ajax,大家下午好:) 我正在开发asp.NETMVC应用程序。 我使用jquerydatatable来显示我的数据。 实际上,我的ajax Post请求调用一个控制器方法,该方法必须接收一些参数。 问题是它们是空的。我还有一个Post 500错误:( 这是我的班级: public class ClientSelectionFiltersViewModel { public bool StrictFilteringContractTypes { get; set; }
public class ClientSelectionFiltersViewModel
{
public bool StrictFilteringContractTypes { get; set; }
public string ContractTypes { get; set; }
public string PaymentChoices { get; set; }
public string PopulationTypes { get; set; }
}
以下是cshtml视图的一部分:
@model ClientSelectionViewModel
@{
ViewBag.Title = "Sélection des clients";
}
<link href="~/lib/datatables/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
<h3><i class="fas fa-clipboard-list mr-3"></i> Sélection des clients</h3>
<div asp-validation-summary="All" class="text-danger mt-2"></div>
<div id="filters">
<form method="post" asp-action="facturationWithFilters">
<!--some code here-->...
</form>
<button id="btn-filter-clients" class="btn atmb-btn-blue"><i class="fas fa-filter fa-fw mr-2"></i>Filtrer les clients</button>
</div>
<div id="client-selection" class="mt-2">
<div class="mb-1">
<a id="btn-send-selection" class="btn float-right atmb-btn-red">
<i class="fas fa-feather-alt fa-fw mr-2"></i>Lancer la facturation pour les utilisateurs sélectionnés
</a>
</div>
<table class="table table-striped table-hover table-sm table-bordered"
id="tableServerSide"
style="width:100%;">
<thead class="thead-dark text-white">
<tr>
<th>Cocher</th>
<th>#</th>
<th>Numéro de client</th>
<th>Types de contrats</th>
<th>Moyen de paiement</th>
<th>Population</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
@section Scripts {
<!--scripts DataTable-->
<script src="~/js/modal.js"></script>
<script src="~/lib/datatables/js/jquery.dataTables.min.js"></script>
<script src="~/lib/datatables/js/dataTables.bootstrap4.min.js"></script>
<!--script pour la gestion de DataTable-->
<script type="text/javascript">
$('#btn-filter-clients').click(function () {
console.log($("#contractFilter").val()); //ok I get my values
console.log($("#paymentFilter").val()); //ok I get my values
console.log($("#populationFilter").val()); //ok I get my values
console.log("début Data Table");
$("#tableServerSide").DataTable({
"processing": true,
"serverSide": true,
"filter": true,
"ajax": {
"url": "/clientselection/ajaxpostcall",
"type": "POST",
"datatype": "json",
"data": JSON.stringify({
filters: {
"ContractTypes": $("#contractFilter").val(),
"PaymentChoices": $("#paymentFilter").val(),
"PopulationTypes": $("#populationFilter").val(),
"StrictFilteringContractTypes": $("#StrictFilteringContractTypes").is(":checked")
}
}),
"contentType": "application/json",
"success": function (response) {
if (response != null) {
alert("Name : " + response.Name + ", Designation : " + response.Designation + ", Location :" + response.Location);
} else {
alert("Something went wrong");
}
},
"failure": function (response) {
alert(response.responseText);
},
"error": function (response) {
alert(response.responseText);
}
},
"columns": [ // on définit les colonnes du tableau
{
//Cases cocher
"data": null,
"className": "col-checkbox",
"render": function (data, type, row) {
var checkId = 'check' + row.id;
var isChecked = selectedIds.includes(checkId) ? ' checked="checked"' : '';
var checkbox = '<input id="' + checkId + '" type="checkbox" '
+ isChecked
+ ' class="server-checkbox" /> ';
/*
<td class="col-checkbox sorting_1">
<input id="check0" type="checkbox" class="server-checkbox">
</td>
*/
return checkbox;
}
},
{
//Numéro de client
"data": "id",
"className": "col-id"
},
{
//Types de contrats
"data": "contractstypes",
"className": "col-contractstypes",
"render": function (data, type, row) {
var chaine = string.Join(", ", row.value);
return chaine;
}
},
{
//Moyen de paiement PaymentChoice
"data": "paymentchoice",
"className": "col-paymentchoice"
},
{
//Population PopulationType
"data": "populationtype",
"className": "col-populationtype"
}
]
});
});
</script>
}
@model ClientSelectionViewModel
@{
ViewBag.Title=“客户选择”;
}
客户选择
...
过滤客户
《柳叶刀》杂志
科谢
#
客户数量
合同类型
莫延德佩因
人口
@节脚本{
$(“#btn筛选器客户端”)。单击(函数(){
console.log($(“#contractFilter”).val();//好的,我得到我的值
console.log($(“#paymentFilter”).val();//好的,我得到我的值
console.log($(“#populationFilter”).val();//好的,我得到我的值
log(“但数据表”);
$(“#表服务器端”).DataTable({
“处理”:对,
“服务器端”:正确,
“过滤器”:正确,
“ajax”:{
“url”:“/clientselection/ajaxpostcall”,
“类型”:“职位”,
“数据类型”:“json”,
“数据”:JSON.stringify({
过滤器:{
“ContractTypes”:$(“#contractFilter”).val(),
“PaymentChoices”:$(“#paymentFilter”).val(),
“PopulationTypes”:$(“#populationFilter”).val(),
“StrictFilteringContractTypes”:$(“#StrictFilteringContractTypes”)为(“:选中”)
}
}),
“contentType”:“application/json”,
“成功”:功能(响应){
if(响应!=null){
警报(“名称:“+response.Name+”,名称:“+response.Designation+”,位置:“+response.Location”);
}否则{
警惕(“出了差错”);
}
},
“故障”:功能(响应){
警报(response.responseText);
},
“错误”:功能(响应){
警报(response.responseText);
}
},
“列”:[//关于画面的柱状图
{
//案例cocher
“数据”:空,
“类名称”:“列复选框”,
“渲染”:函数(数据、类型、行){
var checkId='check'+row.id;
var isChecked=selectedIds.includes(checkId)?“checked=”checked“:“”;
var复选框=“”;
/*
*/
返回复选框;
}
},
{
//客户数量
“数据”:“id”,
“类名称”:“列id”
},
{
//合同类型
“数据”:“合同类型”,
“className”:“col contractstypes”,
“渲染”:函数(数据、类型、行){
var chaine=string.Join(“,”,row.value);
返回链;
}
},
{
//付款方式选择
“数据”:“paymentchoice”,
“类别名称”:“col paymentchoice”
},
{
//人口类型
“数据”:“填充类型”,
“类名称”:“列填充类型”
}
]
});
});
}
以下是我的控制器方法:
[HttpPost]
public async Task<JsonResult> AjaxPostCall(ClientSelectionFiltersViewModel filters)
{
//some code here....
return Json(result);
}
[HttpPost]
公共异步任务AjaxPostCall(客户端选择筛选器视图模型筛选器)
{
//这里有一些代码。。。。
返回Json(结果);
}
我提出了至少6个不适合我的解决方案,例如:
[HttpPost]
public async Task<JsonResult> AjaxPostCall([FromBody] ClientSelectionFiltersViewModel filters)
{
//some code here....
return Json(result);
}
[HttpPost]
公共异步任务AjaxPostCall([FromBody]客户端选择筛选器视图模型筛选器)
{
//这里有一些代码。。。。
返回Json(结果);
}
“contentType”:“application/json”,
"data": JSON.stringify({
"ContractTypes": $("#contractFilter").val(),
"PaymentChoices": $("#paymentFilter").val(),
"PopulationTypes": $("#populationFilter").val(),
"StrictFilteringContractTypes": $("#StrictFilteringContractTypes").is(":checked")
}),
[HttpPost]
public async Task<JsonResult> AjaxPostCall(ClientSelectionFiltersViewModel filters, DataTableRequestParameters parameters)
{
//somecode...
}
var filters = {
ContractTypes: $("#contractFilter").val().join(', '),
PaymentChoices: $("#paymentFilter").val().join(', '),
PopulationTypes: $("#populationFilter").val().join(', '),
StrictFilteringContractTypes: $("#StrictFilteringContractTypes").is(":checked")
}
$("#tableServerSide").DataTable({
"processing": true,
"serverSide": true,
"filter": true,
"ajax": {
//"url": "/clientselection/ajaxpostcall", //doesn't work!!!
"url": "@Url.Action("AjaxPostCall", "ClientSelection")", //ok because of tag helper
"type": "POST",
"data": filters,
......