6

I have a method with the following signature:

public ActionResult RenderFamilyTree(string name, Dictionary<string, string> children)

I'm trying to call it from javascript using jQuery like this:

$('#div_render').load(
    "<%= Url.Action("RenderFamilyTree") %>", 
    { 
         'name': 'Raul',
         [
             {'key':'key1','value':'value1'},
             {'key':'key2','value':'value2'}
         ] 
    }, 
    function() {                
        alert('Loaded');
    }
);

Am I missing something to get this to work?

AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
James Bond
  • 63
  • 1
  • 1
  • 3

2 Answers2

5

There is a syntax error in the javascript object literal. The two key/value pairs in the array should be assigned to a named property alongside "name" (ex: "myProperty").

$('#div_render').load(
"<%= Url.Action("RenderFamilyTree") %>", 
{ 
     name: 'Raul',
     myProperty: [
         {key:'key1',value:'value1'},
         {key:'key2',value:'value2'}
     ] 
}, 
function() {                
    alert('Loaded');
}

);

kwcto
  • 3,494
  • 2
  • 26
  • 33
0

The default model binder supports advanced scenarios such as binding to lists and dictionaries. In order for this to work you need to send the following request:

children[0].Key=key1&children[0].Value=value1&
children[1].Key=key2&children[1].Value=value2&
name=Raul

So you could either write your own custom binder or format your query parameters in this way. I am not sure that jQuery supports this out of the box.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928