Hi,
I'm trying to use the jquery ajax function to post to a MVC2 controller action, but my parameters include an array of a (simple) custom class and the action is not getting the 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 which already work:
1) Using a List<_string> as action parameter and changing the javascript array to a string
array.
2) Using a TestParameterClass as an action parameter and passing 1 instance of the custom class.
So the real trick seems to be getting an array of a custom class passed successfully and with other flat (string) parameters.
Any ideas to make this work? Also is there any documentation on how MVC2 translates the parameter to some C# type (I've used List<> only b/c it seems the most widely used)?
Thanks!