gpt4 book ai didi

c# - 数据是通过 AJAX 传递给操作的对象数组

转载 作者:行者123 更新时间:2023-11-29 18:18:22 25 4
gpt4 key购买 nike

这里我可以通过 AJAX 将数据作为对象数组传递到 Controller ,但我在 Controller 端得到的值为 null。

var newData  = [{Code:"FT",Id:1:Year:2005,TypeId:1,Value:20},
{Code:"FR",Id:2:Year:2006,TYpeId:3,Value:40},
{Code:"FY",Id:3:Year:2007,TYpeId:5,Value:50}]


$.ajax({
url: "api/FG/cretejson",
type: 'POST',
contentType: "application/json",
data: JSON.stringify({ extraParams: newData }),
success: function (data) {
var result = result;
}
});

public JsonResult cretejson([FromBody]List<rev> extraParams)
{
try
{

return Json(new { Result = "OK", Options = extraParams });
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
public class rev
{
public string Id { get; set; }
public string Code { get; set; }
public int TypeId { get; set; }
public int Year { get; set; }
public int Value { get; set; }
}

如有任何帮助,我们将不胜感激。

最佳答案

AJAX 调用:

 $.ajax({
url: urlString,
type: 'POST',
data: sc,
dataType: 'json',
crossDomain: true,
cache: false,
success: function (data) { console.log(data); }
});

Web API Controller :

 [HttpPost]
public string PostProducts([FromBody]List<SyncingControl> persons)
{
return persons.Count.ToString(); // 0, expected 3
}

关于c# - 数据是通过 AJAX 传递给操作的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806791/

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