【问题标题】:How can I send array as a request parameter .Net MVC如何发送数组作为请求参数.Net MVC
【发布时间】:2019-04-13 07:10:49
【问题描述】:

我正在使用 ajax 发送一个字符串数组作为请求参数。但我总是从控制器类接收 null。

dataSource: {
     transport: {
            read: {
              type: "POST",
              data: JSON.stringify(orderList), // list of string
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              url: "@UrlConfig.Action("GetProductListByOrderCodes", "PI")",
             }
      }
 }

我的控制器

[HttpPost]
public HttpResponseMessage GetProductListByOrderCodes( string [] order_codes)
{

}

从 order_codes 参数中获取 null

【问题讨论】:

    标签: c# asp.net ajax kendo-ui


    【解决方案1】:

    您应避免使用JSON.Stringify,并应在请求中添加traditional:true。

    您必须将请求结构更改为以下内容:

            url: "@UrlConfig.Action("GetProductListByOrderCodes", "PI")",
            type: "post",
            dataType: "json",
            contextType: "application/json",
            data: orderList ,
            traditional: true,
            success: function (result) {
                if (result.success) {
                    //Do something useful
                } else {
                    //It all went wrong
                }
            },
            error: function (_err) {
                //It all went REALLY wrong
            }
    

    希望这会有所帮助。

    【讨论】:

    • 你可以添加 json.stringify 然后使用 fiddler 将什么发送到服务器吗?
    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2020-09-07
    • 2019-08-21
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多