gpt4 book ai didi

javascript - jQuery post 不会将数据发布到 ASP.NET API Controller

转载 作者:行者123 更新时间:2023-12-03 11:35:30 24 4
gpt4 key购买 nike

我有一次通过 jquery post 将数据发送到 ASP.NET Controller 的噩梦。这是 JSON.stringify 后数据的样子:

[{"scheduleTaskID":"203","task":"Permit","baselineDate":"4/6/2005 8:00:00 AM","scheduledDate":"4/6/2005 8:00:00 AM","actualDate":"4/6/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"195","task":"Office Files","baselineDate":"7/13/2005 8:00:00 AM","scheduledDate":"7/13/2005 8:00:00 AM","actualDate":"7/13/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"196","task":"Foundation","baselineDate":"7/27/2005 8:00:00 AM","scheduledDate":"7/27/2005 8:00:00 AM","actualDate":"8/13/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"197","task":"Framing","baselineDate":"8/5/2005 8:00:00 AM","scheduledDate":"8/5/2005 8:00:00 AM","actualDate":"8/23/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"198","task":"Finishes Exterior","baselineDate":"8/26/2005 8:00:00 AM","scheduledDate":"8/26/2005 8:00:00 AM","actualDate":"9/14/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"199","task":"Drywall","baselineDate":"9/2/2005 8:00:00 AM","scheduledDate":"9/2/2005 8:00:00 AM","actualDate":"9/16/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"200","task":"Flooring","baselineDate":"9/1/2005 8:00:00 AM","scheduledDate":"9/1/2005 8:00:00 AM","actualDate":"9/20/2005 8:00:00 AM","finishedDate":"","selected":"on"},{"scheduleTaskID":"201","task":"General Finish","baselineDate":"9/12/2005 8:00:00 AM","scheduledDate":"9/12/2005 8:00:00 AM","actualDate":"","finishedDate":"","selected":"on"},{"scheduleTaskID":"202","task":"Final PDI","baselineDate":"10/11/2005 8:00:00 AM","scheduledDate":"10/11/2005 8:00:00 AM","actualDate":"","finishedDate":"","selected":"on"},{"scheduleTaskID":"203","task":"Permit","baselineDate":"4/6/2005 8:00:00 AM","scheduledDate":"4/6/2005 8:00:00 AM","actualDate":"4/6/2005 8:00:00 AM","finishedDate":"","selected":"on"},{}]

这就是我尝试传递数据的方式:

$.post("/api/update/", JSON.stringify( array ), alert('success'), 'json');

这是我的 ASP.NET API Controller :

[HttpPost]
public dynamic Post(List<CellModel> cells)
{
}

这就是 CellModel:

public class CellModel
{
public string scheduleTaskID { get; set; }
public string task { get; set; }
public string baselineDate { get; set; }
public string scheduledDate { get; set; }
public string actualDate { get; set; }
public string finishedDate { get; set; }
public bool selected { get; set; }
}

当我在 public dynamic Post(List<CellModel> cells) 之后在 Controller 中放置断点时它说细胞是cells Count = 0 ...我把return false;在我的帖子调用查看网络调用后,它显示状态代码为 301 永久移动:

enter image description here

这就是我获取这些数据的方式:

$("#form").submit(function (event) {
var array = [];
$('#form > table > tbody > tr').each(function (elem) {
var item = {
scheduleTaskID: $(this).find("td > #scheduleTaskID").val(),
task: $(this).find("td > #task").val(),
baselineDate: $(this).find("td > #baselineDate").val(),
scheduledDate: $(this).find("td > #scheduledDate").val(),
actualDate: $(this).find("td > #actualDate").val(),
finishedDate: $(this).find("td > #finishedDate").val(),
selected: $(this).find("td > #selected").val(),
};
array.push(item);
});

$.post("/api/update/", JSON.stringify(array), alert('success'), 'json');
return false;

});

最佳答案

POST将直接 JSON 数组发送到 MVC Controller 不起作用。解决方案是使用长格式 AJAX jQuery 方法,包括 traditional:true 选项,并以不同的方式构建 JSON 数据模型 - 请参阅 this answer .

关于模型: Controller 操作的参数不应该是数组 - 它应该是一个模型类,其中包含和数组成员 - 当然,您的 JSON 应该反射(reflect)此结构。

关于javascript - jQuery post 不会将数据发布到 ASP.NET API Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530185/

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