【问题标题】:Trying and failing to pass a JavaScript array to my ASP.NET Core controller尝试将 JavaScript 数组传递给我的 ASP.NET Core 控制器但失败
【发布时间】:2020-09-22 11:51:13
【问题描述】:

一直在尝试找出我的 AJAX 调用不起作用的原因。我有数据网格,用户可以在其中选择要删除的行;然后按下删除按钮应将这些选定行的 ID 作为数组发送到我的控制器进行处理。然而,我的控制器只接收一个空的 int 数组。

这是我的代码,如果有人能看出我做错了什么,将不胜感激:

    @(Html.DevExtreme().Button()
          .ID("gridDeleteSelected")
          .Text("Purge Selected")
          .Height(34)
          .Type(ButtonType.Default)
          .StylingMode(ButtonStylingMode.Outlined)
          .Width(120)
          .Disabled(true)
          .OnClick("purgeSelectedBatches")
    ) 

我的相关 JavaScript(当用户在数据网格中选择行时调用 selectedLoadContexts 方法):

<script>
var recordsToBePurged; 

function selectLoadContexts(data) {

    var purgeButton = $("#gridDeleteSelected").dxButton("instance");
    purgeButton.option("disabled", !data.selectedRowsData.length);
    recordsToBePurged = data;   

}

function purgeSelectedBatches() {

    var selectedKeys = recordsToBePurged.selectedRowKeys;
    console.log("selected keys = " + selectedKeys);

    $.ajax({
        url: "@Url.Action("PurgeSelected", "Purge")",
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'JSON', 
        data: JSON.stringify({selectedKeys : selectedKeys}),
        success: function() {

            var url = '@Url.Action("PurgeSelected", "Purge")' +
                '?selectedKeys=' +
                selectedKeys;
            window.location = url;
        }
    }).done(function() {
        DevExpress.ui.notify("Purging selected batches marked as Do Not Retain: ", "info", 1500);
        console.log("Purging selected batches marked as Do Not Retain");
    });

}

</script>

我的控制器方法:

    [HttpPost]
    [Route("[action]")]
    public IActionResult PurgeSelected(int[] selectedKeys)
    {
        
        //process array of keys and remove selected records

        return Ok($" records removed");
    }

【问题讨论】:

    标签: javascript c# ajax asp.net-core devexpress


    【解决方案1】:

    据我所知,recordsToBePurged.selectedRowKeys 是一个逗号分隔的字符串,而不是一个数组。为了使其工作,您需要将字符串转换为整数数组,然后再将其传递给 PurgeController 的 PurgeSelected 方法。所以你的代码会是这样的

    function purgeSelectedBatches() {
    
        var selectedKeys = recordsToBePurged.selectedRowKeys;
        console.log("selected keys = " + selectedKeys);
        //convert the coma seperated value to an int[] before
        var selectedKeysAsArray = selectedKeys.split(',');
        
        $.ajax({
            url: "@Url.Action("PurgeSelected", "Purge")",
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: 'JSON', 
            //pass the values as an array and not as string
            data: JSON.stringify({selectedKeys : selectedKeysAsArray }),
            success: function() {
    
                var url = '@Url.Action("PurgeSelected", "Purge")' +
                    '?selectedKeys=' +
                    selectedKeys;
                window.location = url;
            }
        }).done(function() {
            DevExpress.ui.notify("Purging selected batches marked as Do Not Retain: ", "info", 1500);
            console.log("Purging selected batches marked as Do Not Retain");
        });
    
      }
    
    }
    

    【讨论】:

    • 嗨,感谢您的建议,但我收到控制台错误“未捕获的 TypeError:selectedKeys.split 不是函数”,这让我相信它不是逗号分隔的字符串(抱歉,我的 JS 是不过非常有限)。
    • 你好,试试这样,因为拆分前有一个不必要的空间: var selectedKeysAsArray = selectedKeys.split(',');如果这不起作用,那么这个错误是关于 selectedKeys 不是字符串,我们需要在提出更多建议之前知道它的类型。
    • 谢谢。我相信这是一个int数组。我添加了一行:console.log("第一个选择的键是:" + selectedKeys[0]);并在控制台上得到了这个:选择的第一个键是:2
    【解决方案2】:

    好的,所以我修改了我的 purgeSelectedBatches 方法,剥离 AJAX 调用如下:

    function purgeSelectedBatches() {
    
        var selectedKeys = recordsToBePurged.selectedRowKeys;
        console.log("selected keys = " + selectedKeys);
    
        if (selectedKeys.length() > 0) {
            $.ajax({
                url: "@Url.Action("PurgeSelected", "Purge")",
                type: 'POST',
                dataType: "json",
                data: { selectedKeys: selectedKeys }
    
            }).done(function() {
                DevExpress.ui.notify("Purging selected batches marked as Do Not Retain: ", "info", 1500);
                console.log("Purging selected batches marked as Do Not Retain");
            });
        } else {
            DevExpress.ui.notify("No records to be purged", "info", 1500);
        }
    }
    

    我从数据中删除了 contentType 和 JSON.stringify。控制器现在接收整数数组作为包含所选行的键的参数。

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2017-06-18
      • 2021-11-05
      • 2021-09-24
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多