【问题标题】:Accepting a List from Javascript Ajax Post to C# Method接受从 Javascript Ajax Post 到 C# 方法的列表
【发布时间】: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&lt;Target&gt;并调试。当您看到传入数据的格式时,您就会明白哪里出了问题。附言您可能希望从 OverheadID 属性中删除 virtual...
  • 另外,当你有 dynamic things 这样的参数时,你可以尝试将 dynamic 显式地转换为 List 。如果出现问题,您将获得更有意义的异常。 var items = things.Cast&lt;List&lt;Target&gt;&gt;()var items = things.Cast&lt;Target&gt;()
  • 请确保数据以“[”开头并以“]”结尾,以使其成为 JSON 文件中的列表。然后再次使用 [FromBody] List 东西。

标签: javascript c# json asp.net-core


【解决方案1】:

如果您希望 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) {

        }
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 2012-06-11
    • 2013-01-29
    • 1970-01-01
    • 2011-05-29
    • 2013-11-16
    • 2013-10-21
    • 1970-01-01
    相关资源
    最近更新 更多