0

I have action Method like this :

 [HttpPost]
    public virtual JsonResult AddCombination(Dictionary<Guid,Guid> combinations)
    {

        return Json(new { result = false });
    }

and I have script to send some data with jquery Ajax like this :

 $(document).on('click', '#addcombination', function () {
        var combinationarray = {};

        $(".drpcombination").each(function () {
            var id = $(this).attr('name');
            var selected = $(this).find(':selected');
            if ($(selected).val().trim().length > 0) {
                combinationarray[id] = $(selected).val();
            }
        });

        if (Object.keys(combinationarray).length > 0) {

            $.ajax({
                url: $('#url').data('addcombination'),
                type: 'POST',
                dataType: "json",
                //data: {combinations:combinationarray},
                data:  combinationarray ,
                success: function (data) {

                    alert('success');
                },
                error: function (response) {

                }
            });
        }

    });

always combinatios are null. firebug Result like this : enter image description here

I try some ways but cant get any result .

Uthman Rahimi
  • 708
  • 5
  • 22
  • Is there any specific reason why you use a dictionary. You will find this much easier to post an array of objects.In any case you will at least need to stringify the data and use `contentType: 'json'` –  Apr 25 '16 at 12:34
  • @StephenMuecke In View , I have some dropdown like Attribute and attributeOption , in action method i need AttributeId and AttributeOptionId. I add `contentType:'json'` but don't work – Uthman Rahimi Apr 25 '16 at 12:38
  • you cannot pass dictionary parameter. Here is what you can do. Create a class eg.MyClass with 2 properties of guid type. Your function parameter will be of type List. From client create the json array. Once you get the data in the method, convert to dictionary. – Karthik M R Apr 25 '16 at 12:40
  • 1
    `var combinationarray = []; combinationarray.push({ AttributeId: 1, AttributeOptionId: 2 };` and `data: JSON.stringify({combinations: combinationarray}),` and `contentType:'json'` will post back to `List combinations` where `MyModel` contains those 2 properties –  Apr 25 '16 at 12:40
  • If you do want a dictionary, refer [this answer](http://stackoverflow.com/questions/28297075/transmit-javascript-object-into-controller-action-as-dictionary/28298048#28298048) for one option –  Apr 25 '16 at 12:48
  • @StephenMuecke I add viewmodel like this `AddCombination(List combinations)` but its null , and add ` contentType: 'json', data: JSON.stringify({ combinations: combinationarray }),` – Uthman Rahimi Apr 25 '16 at 12:53
  • and this ViewModel :` public class testvm { public Guid AttributeId { get; set; } public Guid AttributeOptionId { get; set; } }` – Uthman Rahimi Apr 25 '16 at 12:54
  • @UthmanRahimi, The code in my previous comment does work, so you have not done it correctly. –  Apr 25 '16 at 12:54
  • @StephenMuecke see https://jsfiddle.net/e5bqdmsh/ – Uthman Rahimi Apr 25 '16 at 12:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110156/discussion-between-uthman-rahimi-and-stephen-muecke). – Uthman Rahimi Apr 25 '16 at 12:59
  • @StephenMuecke thanks for help , I forgot add `contentType: 'application/json; charset=utf-8',` , thanks a lot – Uthman Rahimi Apr 26 '16 at 05:34

0 Answers0