gpt4 book ai didi

c# - MVC 核心 Controller 中的对象数组始终为 null

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:28 24 4
gpt4 key购买 nike

我正在尝试使用 Frombody 属性将一个简单对象的数组发布到我的 MVC 核心 Controller ,但由于某种原因它变为 null。如果我只发布一个对象,它工作正常,这是我的代码:

$(document).ready(function() {
var tk = new Array();

$("#calendar").fullCalendar({

header: {
left: 'prevYear,prev,next,nextYear',
center: 'title',
right: 'month,agendaWeek,agendaDay',
},

defaultView: 'month',
editable: true,
alldayslot: false,
selectable: true,
slotminutes: 15,
nextDayThreshold: "00:00:00",
events: "/Home/FullCalendar",
eventDrop: function(event, delta, revertFunc)
{
Update(event);
},
});

function Update(event) {
var datarow =
{
"id": event.id,
"start": event.start,
"end": event.end,
}
tk.push(datarow);

debugger;
confirm(("Save changes?"))
console.log(JSON.stringify(datarow));
$.ajax({
type: "post",
dataType: 'json',
cache: false,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(tk),
url: '@Url.Action("JSON", "Home")',
success: function (data) {
debugger;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
debugger;
}
});
}
});

我的 Controller 类

[HttpPost]
public IActionResult UpdateTask([FromBody] List<Task_fullcal> td)
{
// Do something
// td is always null if passed as an array
//td working fine if passed as single value
}

最佳答案

我遇到了同样的问题,我花了几个小时才弄清楚。我把解决方案写在了

Pass array of objects to controller by jQuery AJAX, action data is always null in ASP.NET Core 2.2 MVC - it is working with .NET Framework 4.5

遵循以下:


我在 .Net Core 3.1 中工作,这对我有用:

<强>1。 Controller

尝试用[FromForm]代替[FromBody]

    [HttpPost]
public ActionResult CreateOrdersPOSTObject([FromForm] List<T_POS_ENT_ORDER_DETIALS_Temp> prm)
{
//your logic...
return Json(new { msg = "Successfully added " });
}

<强>2。 AJAX 调用

不应使用 JSON.stringfy()contentType

            var DATA = [];
DATA.push({ LocPrice: "12", LocProductID: "1002", discount: "0", posNumber: "1", productName: "soap", productQynt: "1" });
DATA.push({ LocPrice: "23", LocProductID: "1003", discount: "0", posNumber: "1", productName: "shampoo", productQynt: "1" });
DATA.push({ LocPrice: "7",LocProductID: "1004" ,discount: "0",posNumber: "2" ,productName: "sponge",productQynt: "2"});

$.ajax({
url: '@Url.Content("~/home/CreateOrdersPOSTObject")',
data: { "prm": DATA },
type: "POST",
dataType: "json",
async: true
});

或者,如果您愿意,可以使用简写 $.post 来获得相同的结果

            var DATA = [];
DATA.push({ LocPrice: "12", LocProductID: "1002", discount: "0", posNumber: "1", productName: "soap", productQynt: "1" });
DATA.push({ LocPrice: "23", LocProductID: "1003", discount: "0", posNumber: "1", productName: "shampoo", productQynt: "1" });
DATA.push({ LocPrice: "7",LocProductID: "1004" ,discount: "0",posNumber: "2" ,productName: "sponge",productQynt: "2"});

$.post(
'@Url.Content("~/home/CreateOrdersPOSTObject")',
{ "prm": DATA },
function () {
alert( "success" );
})
.fail(function() {
alert( "error" );
});

关于c# - MVC 核心 Controller 中的对象数组始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40056240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com