C# 从复选框中获取值,并作为列表发布到控制器

C# 从复选框中获取值,并作为列表发布到控制器,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,检查下面的代码。我正在向我的控制器发布一个ProductId列表,并希望通过名为-UpdateProductStatus的视图模型接收它。但是我当前代码的问题是:ajax成功地将其发布到controller,但是UpdateProductStatus无法获取ProductId的任何值。这总是返回null。我在这里干什么?我想在阿贾克斯我可能做错了。如何修复此问题以将所有ProductId作为列表表单控制器接收?提前谢谢 控制器: [HttpPost] public IAction

检查下面的代码。我正在向我的控制器发布一个
ProductId
列表,并希望通过名为-
UpdateProductStatus
的视图模型接收它。但是我当前代码的问题是:ajax成功地将其发布到controller,但是
UpdateProductStatus
无法获取
ProductId
的任何值。这总是返回null。我在这里干什么?我想在阿贾克斯我可能做错了。如何修复此问题以将所有
ProductId
作为列表表单控制器接收?提前谢谢

控制器:

[HttpPost]
        public IActionResult UpdateProductStatus([FromBody]List<UpdateProductStatus> UpdateProductStatus)
        {



            return Json("ok");
        }
Jquery:

 $('#btnActivate').on('click', function () {


            var allSelectedProductId = [];
            var allSelectedProductIdWithKey = [];
            $('.chkItems:checked').each(function() {
                allSelectedProductId.push($(this).val());
            });

            for (var _allSelectedProductId in allSelectedProductId) {
                allSelectedProductIdWithKey.push('ProductId'+':'+_allSelectedProductId);
            }

            console.log(allSelectedProductIdWithKey);



            //var things = JSON.stringify(allSelectedProductIdWithKey);
            var things = JSON.stringify({ 'UpdateProductStatus': allSelectedProductIdWithKey });


            console.log(things);

            $.ajax({
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: 'POST',
                url: '/Products/UpdateProductStatus',
                data: things,
                success: function () {
                    console.log('sssss');
                },
                failure: function (response) {
                    console.log('fffff');
                }
            });
Html:


按钮

要绑定到控制器方法,需要发送一个包含
ProductId
的名称/值对的对象数组。要构建对象数组,请使用

$('#btnActivate').on('click', function () {
    var allSelectedProductId = [];
    $('.chkItems:checked').each(function() {
        allSelectedProductId.push({ ProductId: $(this).val() });
    });
    var things = JSON.stringify({ UpdateProductStatus: allSelectedProductId });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Products/UpdateProductStatus',
        data: things,
        success: function () {
            ....
    });
});

您当前的代码正在为ajax调用发送一个如下所示的负载

{"UpdateProductStatus":["ProductId:0","ProductId:1"]}
动作方法参数是
UpdateProductStatus
对象的列表。因此,为了使模型绑定能够与您当前的操作方法参数签名一起正常工作,您的负载应该如下所示

[{"ProductId":"1"},{"ProductId":"2"}]
无需指定参数名称。只需传递一个项目数组,每个项目都有一个
ProductId
属性及其值

var allSelectedProductIdWithKey = [];
$('.chkItems:checked').each(function () {
    allSelectedProductIdWithKey.push({ ProductId: $(this).val() });
});

var things = JSON.stringify(allSelectedProductIdWithKey);

$.ajax({
    contentType: 'application/json; charset=utf-8',
    type: 'POST',
    url: '/Products/AppendClientFilter',
    data: things,
    success: function (res) {
        console.log('Successs', res);
    },
    failure: function (response) {
        console.log('Error', response);
    }
});
您还可以在ajax调用中删除
数据类型。jqueryajax将从响应头猜测正确的类型,在您的例子中,您将显式返回JSON

[{"ProductId":"1"},{"ProductId":"2"}]
var allSelectedProductIdWithKey = [];
$('.chkItems:checked').each(function () {
    allSelectedProductIdWithKey.push({ ProductId: $(this).val() });
});

var things = JSON.stringify(allSelectedProductIdWithKey);

$.ajax({
    contentType: 'application/json; charset=utf-8',
    type: 'POST',
    url: '/Products/AppendClientFilter',
    data: things,
    success: function (res) {
        console.log('Successs', res);
    },
    failure: function (response) {
        console.log('Error', response);
    }
});