var formData1 = $("#form1").serializeObject();
var formData2 = $("#form2").serializeObject();
$.extend(formData1, formData2);
var formData = JSON.stringify(formData1);
$.ajax({
type: "POST",
url: "@Url.Action("MyAction", "MyController")",
data: formData,
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (data) {
...Do something with the data
},
error: function(result) {
...Handle the error
}
});
Then on the side of your controller (I use MVC, but WebAPI will probably work the same way) you can declare two separate parameters that correspond to your client models, and everything will be parsed for you, that is, it is assumed that t have matching property names! Gotta love him when magic happens!
public ActionResult MyAction(FormDataModel1 formData1, FormDataModel2 formData2)
Confirm https://github.com/macek/jquery-serialize-object for serializeObject code.
source
share