ASP.NET 核心发布数组对象 JSON
ASP.NET Core Posting Array Object JSON
我正在尝试 post 我的控制器视图中的一个对象数组,但参数为 null 我看到对于一个简单的对象,我需要将 [FromBody] 放入我的控制器操作中。
这是我的 JSON:
{
"monJour": [
{
"openTime": "04:00",
"closedTime": "21:30",
"id": "0"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "1"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "2"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "3"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "4"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "5"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "6"
}
]
}
这是我的 Ajax 请求:
function SendAllDay() {
var mesJours = {};
var monJour = [];
mesJours.monJour = monJour;
var senddata ='';
$('div[id^="Conteneur"]').each(function () {
var idDiv = $(this).attr("id");
var jour = idDiv.substr(9, idDiv.length - 9);
var opentps = $("#O" + jour).val();
var closetps = $("#C" + jour).val();
var monid = $("#id" + jour).val();
monJour = {
openTime: opentps,
closedTime: closetps,
id: monid
}
mesJours.monJour.push(monJour);
});
$.ajax({
url: '@Url.Action("ReceiveAll")',
dataType: 'json',
type: 'POST',
//data: JSON.stringify(monJours),
data: JSON.stringify(mesJours),
contentType:'application/json',
success: function (response) {
console.log('ok');
//window.location.reload();
},
error: function (response) {
console.log('error');
//alert(response)
//window.location.reload();
}
});
}
这是我的操作:
[HttpPost]
public void ReceiveAll([FromBody]ReceiveTime [] rt) { }
这是我的 Class:
public class ReceiveTime
{
public string openTime { get; set; }
public string closedTime { get; set; }
public string id { get; set; }
}
感谢任何帮助:)
ajaxjs请求中的名称需要与后台controller传来的参数名称一致
尝试按如下方式更新控制器中的方法签名:
public void ReceiveAll([FromBody]ReceiveTime [] monJour)
此外,尽管我喜欢法语,但我还是建议您使用英语命名变量。无论如何请不要被冒犯,但真的很推荐。
与其使用 ReceiveTime[] rt
,不如根据与 POST 相同的结构对数据建模会更好。例如,您可以创建如下所示的 class:
public class MesJours
{
public ReceiveTime[] MonJour { get; set; }
}
我不知道 MesJours
作为 class 名称是否有意义(我不会说法语),但这个想法仍然很清楚 - 你可以命名 class 随心所欲
鉴于此,您可以像这样更新您的控制器:
[HttpPost]
public void ReceiveAll([FromBody] MesJours mesJours)
{
// Access monJour via mesJours.
var rt = mesJours.MonJour;
}
这将满足 ASP.NET MVC Model Binder 并且应该为您提供您已经 POST 编辑的数据。这还有一个额外的好处,可以轻松容纳您可能想要 POST.
的任何其他属性
1.Add一个class,老师:
public class MesJours
{
public ReceiveTime[] MonJour { get; set; }
}
2.And 在控制器操作中将 return 类型更改为异步任务(对于 aspnetcore)
[HttpPost]
public async Task<IActionResult> ReceiveAll([FromBody]MesJours mesJourData)
{
var rt = mesJourData.MonJour;
}
3.in 视图,确保发布的数据与控制器操作中的参数同名(在本例中为 mesJourData)。注意ajax方法
中包含数据参数的行的语法
$.ajax({
url: '@Url.Action("ReceiveAll")',
dataType: 'json',
type: 'POST',
data:{mesJourData: mesJours} ,
contentType:'application/json',
success: function (response) {
console.log('ok');
//window.location.reload();
},
error: function (response) {
console.log('error');
//alert(response)
//window.location.reload();
}
});
我正在尝试 post 我的控制器视图中的一个对象数组,但参数为 null 我看到对于一个简单的对象,我需要将 [FromBody] 放入我的控制器操作中。
这是我的 JSON:
{
"monJour": [
{
"openTime": "04:00",
"closedTime": "21:30",
"id": "0"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "1"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "2"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "3"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "4"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "5"
},
{
"openTime": "08:00",
"closedTime": "17:30",
"id": "6"
}
]
}
这是我的 Ajax 请求:
function SendAllDay() {
var mesJours = {};
var monJour = [];
mesJours.monJour = monJour;
var senddata ='';
$('div[id^="Conteneur"]').each(function () {
var idDiv = $(this).attr("id");
var jour = idDiv.substr(9, idDiv.length - 9);
var opentps = $("#O" + jour).val();
var closetps = $("#C" + jour).val();
var monid = $("#id" + jour).val();
monJour = {
openTime: opentps,
closedTime: closetps,
id: monid
}
mesJours.monJour.push(monJour);
});
$.ajax({
url: '@Url.Action("ReceiveAll")',
dataType: 'json',
type: 'POST',
//data: JSON.stringify(monJours),
data: JSON.stringify(mesJours),
contentType:'application/json',
success: function (response) {
console.log('ok');
//window.location.reload();
},
error: function (response) {
console.log('error');
//alert(response)
//window.location.reload();
}
});
}
这是我的操作:
[HttpPost]
public void ReceiveAll([FromBody]ReceiveTime [] rt) { }
这是我的 Class:
public class ReceiveTime
{
public string openTime { get; set; }
public string closedTime { get; set; }
public string id { get; set; }
}
感谢任何帮助:)
ajaxjs请求中的名称需要与后台controller传来的参数名称一致
尝试按如下方式更新控制器中的方法签名:
public void ReceiveAll([FromBody]ReceiveTime [] monJour)
此外,尽管我喜欢法语,但我还是建议您使用英语命名变量。无论如何请不要被冒犯,但真的很推荐。
与其使用 ReceiveTime[] rt
,不如根据与 POST 相同的结构对数据建模会更好。例如,您可以创建如下所示的 class:
public class MesJours
{
public ReceiveTime[] MonJour { get; set; }
}
我不知道 MesJours
作为 class 名称是否有意义(我不会说法语),但这个想法仍然很清楚 - 你可以命名 class 随心所欲
鉴于此,您可以像这样更新您的控制器:
[HttpPost]
public void ReceiveAll([FromBody] MesJours mesJours)
{
// Access monJour via mesJours.
var rt = mesJours.MonJour;
}
这将满足 ASP.NET MVC Model Binder 并且应该为您提供您已经 POST 编辑的数据。这还有一个额外的好处,可以轻松容纳您可能想要 POST.
的任何其他属性1.Add一个class,老师:
public class MesJours
{
public ReceiveTime[] MonJour { get; set; }
}
2.And 在控制器操作中将 return 类型更改为异步任务(对于 aspnetcore)
[HttpPost]
public async Task<IActionResult> ReceiveAll([FromBody]MesJours mesJourData)
{
var rt = mesJourData.MonJour;
}
3.in 视图,确保发布的数据与控制器操作中的参数同名(在本例中为 mesJourData)。注意ajax方法
中包含数据参数的行的语法$.ajax({
url: '@Url.Action("ReceiveAll")',
dataType: 'json',
type: 'POST',
data:{mesJourData: mesJours} ,
contentType:'application/json',
success: function (response) {
console.log('ok');
//window.location.reload();
},
error: function (response) {
console.log('error');
//alert(response)
//window.location.reload();
}
});