1

I have a key-value pair through which I send down values like this:

   $(document).on('click', '.chkAll', function () {
        $("#tableProducts thead tr th input:checkbox").change(function () {
            $(".chkItem").prop('checked', $(this).prop("checked"));
            if ($(this).prop('checked')) {
                $('tbody tr td input[type="checkbox"]').each(function () {
                    product_ids[$(this).attr('id')] = "Checked";
                    productLocation[$(this).attr('id')] = $(this).attr('location');
                });
            } else {
                $('tbody tr td input[type="checkbox"]').each(function () {
                    delete product_ids[$(this).attr('id')];
                    delete productLocation[$(this).attr('id')];
                });
            }
        });

And this is how I post all the keys into the .NET MVC Action:

values: Object.keys(product_ids)

And fetching it in Controller:

public ActionResult GetValues(List<string> values)
{
// now I have all the keys here but now I'd like values as well...
}

The way I imagine this to be done is by passing down the entire name-value pair array directly into the function and then fetching it, so that then I'd have:

// key (product id) => 0124914815DD29
// value => checked 

Both strings ...

I've tried by doing simply like this:

values: product_ids // in jquery post

And then fetching the value like this:

public ActionResult GetValues(Dictionary<string,string> values)
{
// but now the values dictionary is always empty
}

How can I do this?

Edit:

This is the part where the data is posted:

$(".btnAnalyze").click(function () {
            if (jQuery.isEmptyObject(product_ids) == true) {
                ShowMessage("You haven't selected any product!");
                return;
            }
            else {
                var guid = '@Guid.NewGuid()'
                var postData = { values: Object.keys(product_ids), drange: $('.range').val(), ID: guid, type: $('input[name=type]:checked').val(), shipping: $('input[name=shipping]:checked').val(), condition: $('input[name=condition]:checked').val(), minprice: $('.txtmin').val(), maxprice: $('.txtmax').val(), Title: $('.txtSearch').val(), negative: $('.txtNegativeKeywords').val(),locations: productLocation };
                $.ajax({
                    type: "POST",
                    url: "/Analyze/GetAllProducts",
                    data: postData,
                    success: function (data) {
                        if (data == "Error") {
                            alert("Something went wrong");
                        } else if (data=="Ok"){
                            window.location.href = '/Analyze/Index/' + guid // redirect to another page
                        }
                    },
                    dataType: "json",
                    traditional: true
                });
            }
        });

And mapped header of the Action:

  [HttpPost]
        public async Task<JsonResult> GetAllProducts(List<string> values, string drange, string ID, string type, string shipping, string condition, string minprice, string maxprice, string Title,string negative,string minFeedback, string maxFeedback, Dictionary<string,string> locations)

@Haim Please see the last parameter that I'm passing into function , that's the one I'm trying to turn into a dictionary...

User987
  • 3,663
  • 15
  • 54
  • 115

1 Answers1

1

Apparently, the default model binder expects the following format when trying to bind a Dictionary:

locations[1]=val1&locations[2]=val2

But using jQuery's traditional parameterization you'll get the following instead:

locations=[object+Object]

See https://stackoverflow.com/a/5497151/1625737

Community
  • 1
  • 1
haim770
  • 48,394
  • 7
  • 105
  • 133
  • Side note: The format you have shown only works for a `Dictionary` where the key and value are simple value types or `string`. Other wise, the format is `name[indexer].Key=value&name[indexer].Value.propertyName=value&...` –  Jan 22 '17 at 22:04