help using jquery .ajax() method to pass aggregate data to mvc2 action -


i'm trying use jquery ajax function post mvc2 controller action, parameters include array of (simple) custom class , action not getting data correctly.

client:

var element1 = { firstname: 'raymond', lastname: 'burr' }; var element2 = { firstname: 'johnny', lastname: 'five' }; var var2 = [element1, element2];  var var1 = 'some string';     var parms = {   var1: var1,   var2: var2 }; var ajaxargs = {     type: "post",     traditional: true,     url: "/home/test1",     data: parms,     datatype: "json",     success: returnsuccess,     error: returnerror };  $.ajax(ajaxargs); 

server:

[httppost]                                                                    public actionresult test1(string var1, list<testparameterclass> var2) { ... }  public class testparameterclass               {                                           public string firstname { get; set; }   public string lastname { get; set; }  } 

2 cases work: 1) using list<_string> action parameter , changing javascript array string array.
2) using testparameterclass action parameter , passing 1 instance of custom class.

so real trick seems getting array of custom class passed , other flat (string) parameters.

any ideas make work? there documentation on how mvc2 translates parameter c# type (i've used list<> b/c seems used)?

thanks!

ah, model binder. source of magic , cause of woe when comes .net mvc. model binder turns parameters objects, , it's little picky pass it.

in particular case, think want pass parameters in form

"people[0].firstname": "raymond", "people[0].lastname": "burr", "people[1].firstname": "johnny", "people[1].lastname": "five" 

basically, if you're passing model binder collection type, wants parameters named array-style indices (bracket-number) , property name. @ least that's what's worked me; i'm no mvc ninja.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -