0

I have an jquery key-value pair array as following:

  • 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:

  $(".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...

Can someone help me out with this???

5
  • Anyony has any idea how to do this ??? Commented Oct 16, 2016 at 20:40
  • First of all, slow down. Stack Overflow is not your personal answer slave. Secondly, product_ids is not an array; it's an object with keys that are your IDs. You might do some research on Object.keys(). Commented Oct 16, 2016 at 20:41
  • Could you provide a more reasonable answer... I do realize it's not y personal answer slave, nor I ever considered that to be... Now onto my first question, can you provide an answer so that I can accept it if it's correct... Commented Oct 16, 2016 at 20:44
  • 1
    You certainly imply that it is when you ask for answers less than three minutes after you've posted the question... Please read How to Ask and take the tour. Why don't you try applying the hint I gave you? Object.keys(product_ids). Commented Oct 16, 2016 at 20:48
  • Well my apologies if you understood it like that, I didn't mean it that way... And yes I just tried your hint and it works like a charm! :) It'd be better if you wrote an answer so I could accept it... Commented Oct 16, 2016 at 20:53

2 Answers 2

1

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):

    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 :).

Sign up to request clarification or add additional context in comments.

Comments

0

Use console.log(data.Result) to debug instead of alert(data.Result), because alert(data.Result) stringifies your variable to [object Object]. To open up console press F12

1 Comment

Can you show what you receive on the server side and back at ajax success callback?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.