pass dictionary to controller asp.net mvc

前端 未结 3 1639
臣服心动
臣服心动 2020-12-15 05:39

I am wanting to pass a dictionary of type to my controller via an Ajax post. The main reason here is the post may have between 1-3 key value pai

3条回答
  •  臣服心动
    2020-12-15 05:55

    Something like (javascript)

    dict = new Object();
    dict['12'] = 5;
    dict['13'] = 6;
    dict['1000'] = 21;
    dict['9'] = 13;
    dict['13'] = 48;
    
    $.post('/client.mvc/mypostaction/', { myDictionary: dict });
    

    You can then post the dict object to your controller using a Dictionary as property type.

    ActionResult MyPostAction(Dictionary myDictionary)
    

    edit from author's code second time:

    The following works for me, when having a Dictionary kvPairs. isn't going to work after all.

    Make your post like:

    var dict = new Object();
    dict['13'] = 9;
    dict['14'] = 10;
    dict['2'] = 5;
    
    $.post('controller.mvc/Test', { 'kvPairs': dict }, function(obj) { $('#output').html(obj.Count); }); 
    

提交回复
热议问题