简体   繁体   English

将键值对jquery数组传递到.NET MVC操作

[英]Passing key-value pair jquery array into .NET MVC action

I have an jquery key-value pair array as following: 我有一个jQuery键值对数组,如下所示:

  • The array is filled when check-boxes are checked 选中复选框时,将填充数组
  • If a checkbox is unchecked, item will be removed from there 如果未选中此复选框,则将从此处删除项目

The code for that part is: 该部分的代码是:

  $(document).on('click', '.chkItem', function () {
            if ($(this).is(':checked')) {
                product_ids[$(this).attr('id')] = "Checked";
            } else {
                delete product_ids[$(this).attr('id')];
            }
        });

This works fine, let's head onto the next part. 效果很好,让我们继续下一部分。 Now I'm trying to send all these ID's from my view into a controller "Analyze" and action "Index", which looks as following: 现在,我尝试将所有这些ID从我的视图发送到控制器“ Analyze”和操作“ Index”中,如下所示:

  $(".btnAnalyze").click(function () {
            if (jQuery.isEmptyObject(product_ids) == true) {
                alert("Array is empty");
            }
            else {

                var postData = { values: product_ids };

                $.ajax({
                    type: "POST",
                    url: "/Analyze/Index",
                    data: postData,
                    success: function (data) {
                        alert(data.Result);
                    },
                    dataType: "json",
                    traditional: true
                });

            }
        });

And my action looks like following: 我的动作如下所示:

 public ActionResult Index(List<string> values)
        {
            string id2= "";
            foreach (var id in values)
            {
                id2= id;
            }
// I'm trying to fetch the ID values here that I added into the array, but I'm not sure how... 

}

With this current method the contents of List values is 使用这种当前方法,列表值的内容是

"[object Object]" 

which is not what I want.. I need all of the ID's sorted out nicely into the list as I send them to the Action Index... 这不是我想要的。当我将所有ID发送到操作索引时,我需要将所有ID很好地整理到列表中...

Can someone help me out with this??? 有人可以帮我这个吗???

You're using an object, not an array, and it is thus saying that it's an object. 您使用的是对象,而不是数组,因此它表示它是一个对象。 You have two choices: 您有两种选择:

  1. Use an actual array: 使用实际的数组:

     var product_ids = []; $(document).on('click', '.chkItem', function () { if ($(this).is(':checked')) { product_ids.push($(this).attr('id')); } else { product_ids.splice(product_ids.indexOf($(this).attr('id')), 1); } }); 
  2. Continue using an object and use Object.keys to get just the property names (which are the IDs in your code): 继续使用对象,并使用Object.keys仅获取属性名称(它们是代码中的ID):

     var postData = { values: Object.keys(product_ids) }; 

Personally, I like #1 because it's more explicit that you're capturing the ids of the checked checkboxes, and the values of the object's properties is always "Checked" rather than anything meaningful, but hey, JavaScripts' flexible like that :). 就个人而言,我喜欢#1,因为它更明确地表明您正在捕获已选中复选框的ID,并且对象属性的值始终为"Checked"而不是任何有意义的东西,但是,嘿,JavaScript很灵活:)。

Use console.log(data.Result) to debug instead of alert(data.Result) , because alert(data.Result) stringifies your variable to [object Object] . 使用console.log(data.Result)而不是alert(data.Result) console.log(data.Result)进行调试,因为alert(data.Result)将变量字符串化为 [object Object] To open up console press F12 要打开控制台,请按F12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM