如何将数组作为请求参数 .Net MVC 发送



我正在使用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 参数获取空

您应该避免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
        }

希望这有帮助。

最新更新