【发布时间】:2020-02-18 21:58:29
【问题描述】:
我想知道如何将有效目标列表从 Javascript 传递到 C# 方法 PassThings()。
例如,来自 Javascript $.ajax 帖子的类似内容。
{
"OverheadID": "31l",
"ValuationClassID": 1,
"InventoryElementID": 1,
"Target_A_LC": 0,
"Target_A_QTY": 0,
"Target_B_LC": null,
"Target_B_QTY": null,
"TargetDescription": null
},
{
"OverheadID": "31l",
"ValuationClassID": 1,
"InventoryElementID": 2,
"Target_A_LC": 0,
"Target_A_QTY": 0,
"Target_B_LC": null,
"Target_B_QTY": null,
"TargetDescription": null
},
{
"OverheadID": "31l",
"ValuationClassID": 1,
"InventoryElementID": 3,
"Target_A_LC": 0,
"Target_A_QTY": 0,
"Target_B_LC": null,
"Target_B_QTY": null,
"LopTargetDescription": null
},
{
"OverheadID": "31l",
"ValuationClassID": 2,
"InventoryElementID": 1,
"Target_A_LC": 0,
"Target_A_QTY": 0,
"Target_B_LC": null,
"Target_B_QTY": null,
"TargetDescription": null
}
我无法从我的 Javascript $.ajax 帖子到 C# 方法 PassThings() 获取任何类型的列表。
我只能对变量 OneOfNine 进行硬编码,它表示 JSON 格式的单个有效 Target。
这是为了确认 Javascript 将发送有效的语法并且我的 JsonConvert.DeserializeObject<Target>(OneOfNine) 将起作用。
public class Target{
public virtual string OverheadID { get; set; } //not a true Fkey
public int ValuationClassID { get; set; } //not a true Fkey
public int InventoryElementID { get; set; } //not a true Fkey
public decimal? Target_A_LC { get; set; } //LOP Target A Local Currency
public decimal? Target_A_QTY { get; set; } //LOP Target A Qty
public decimal? Target_B_LC { get; set; } //Target B Local Currency
public decimal? Target_B_QTY { get; set; } //LOP Target B Qty
public string TargetDescription { get; set; } //optional text reference
}
public IActionResult PassThings()
{
var OneOfNine = "{ \"OverheadID\": \"ASASAS\", \"ValuationClassID\": 2, \"InventoryElementID\": 3, \"Target_A_LC\": 0, \"Target_A_QTY\": 0, \"Target_B_LC\": 0, \"Target_B_QTY\": 0, \"TargetDescription\": \"na\" }";
var deserial = JsonConvert.DeserializeObject<Target>(OneOfNine);
var resource =
new Target
{
OverheadID = deserial.OverheadID,
ValuationClassID = deserial.ValuationClassID,
InventoryElementID = deserial.InventoryElementID,
Target_A_LC = deserial.Target_A_LC,
Target_A_QTY = deserial.Target_A_QTY,
Target_B_LC = deserial.Target_B_LC,
Target_B_QTY = deserial.Target_B_QTY,
TargetDescription = deserial.TargetDescription
};
return Ok(resource);
}
我试过这样设置
public IActionResult PassThings([FromBody]List<Target> things)
还有我的这种格式的 Javascript 代码。
$.ajax({
type: 'POST',
//contentType: 'application/json; charset=utf-8',
accepts: 'application/json', //mandatory activity
contentType: 'application/json', //mandatory activity
url: '/api/APILopTargets/PassThings',
data: JSON.stringify(things)
});
【问题讨论】:
-
尝试接受它作为一个数组而不是一个列表——如果你需要使用列表操作,你可以随时转换它:
public IActionResult PassThings([FromBody]Target[] things) -
用
[FromBody]dynamic替换[FromBody]List<Target>并调试。当您看到传入数据的格式时,您就会明白哪里出了问题。附言您可能希望从 OverheadID 属性中删除virtual... -
另外,当你有
dynamic things这样的参数时,你可以尝试将 dynamic 显式地转换为 List。如果出现问题,您将获得更有意义的异常。 var items = things.Cast<List<Target>>()或var items = things.Cast<Target>() -
请确保数据以“[”开头并以“]”结尾,以使其成为 JSON 文件中的列表。然后再次使用 [FromBody] List
东西。
标签: javascript c# json asp.net-core