javascript - 接受从 Javascript Ajax Post 到 C# 方法的列表
问题描述
我想知道如何将有效目标列表传递给PassThings()
从 Javascript 到 C# 的方法。例如,来自 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
}
$.ajax
从我的 Javascript帖子到 C# 方法,我一直无法获得任何类型的列表PassThings()
。
我只能对代表JSON 格式OneOfNine
的单个有效变量的变量进行硬编码。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)
});
解决方案
如果您希望 url 为/api/APILopTargets/PassThings
,则需要将 ApiController 配置为
[Route("api/[controller]")]
[ApiController]
public class APILopTargetsController : ControllerBase
{
[HttpPost("PassThings")]//add the attribute
public IActionResult PassThings([FromBody]List<Target> things)
{...}
}
JavaScript:
var things = [
{
"OverheadID": "31l",
"ValuationClassID": 1,
"InventoryElementID": 1,
"Target_A_LC": 0,
"Target_A_QTY": 0,
"Target_B_LC": null,
"Target_B_QTY": null,
"TargetDescription": null
},
{...},
...
];
$.ajax({
contentType: 'application/json; charset=utf-8',
type: 'POST',
url: '/api/APILopTargets/PassThings',
data: JSON.stringify(things),
success: function (result) {
}
});
推荐阅读
- sql-server - 将日期与特定日期格式进行比较
- c# - 通过 ICollectionView 过滤 ObservableCollection 并使用它
- docker - 无法按名称访问虚拟主机域 docker apache - 一个灯堆栈应用程序
- react-native - polidea ble编码发送包到base64
- javascript - 提供的注册令牌未注册。以前有效的注册令牌可能由于多种原因而被注销
- php - 为什么 isPasswordValid() 函数总是返回 false?
- python - Plotly:如何在不同的轨迹上绘制带有时间值的标记?
- htmlwidgets - visNetwork 可以将 html 依赖项(javascript 库等)保存到特定文件夹吗?
- performance - 本地搜索分数计算 10 / 秒是否正常?
- c# - 这个错误每次都会发生。我该怎么办?