I want to send an array of objects with other data forms to an MVC action using ajax.
var arr = [
{ Name: "a", IsActive: "true" },
{ Name: "b", IsActive: "false" },
{ Name: "c", IsActive: "true" },
{ Name: "d", IsActive: "false" },
{ Name: "e", IsActive: "true" }
];
and some other data form fields. I tried to get and attach the form data as follows:
$(document.body).delegate("form#mainFormCreate", "submit", function () {
var formData = new FormData($("form#mainFormCreate")[0]);
var arr = [
{ Name: "a", IsActive: "true" },
{ Name: "b", IsActive: "false" },
{ Name: "c", IsActive: "true" },
{ Name: "d", IsActive: "false" },
{ Name: "e", IsActive: "true" }
];
jQuery.each(arr, function (key, value) {
formData.append('Features[' + key + ']', value);
});
$.ajax({
url: '@Url.Action("CreateType", "Admin")',
type: 'POST',
data:formData,
success: function (result) {
....
},
error: function (x, t, e) {
....
},
cache: false,
contentType: false,
processData: false
});
return false;
});
But on the server side, I get an array of 5 elements with zero values ββin the Features property. Here is my server side code. my object:
public class ProductTypeCreateModel
{
public string ProductTypeName { get; set; }
public List<Feature> Features { get; set; }
}
public class Feature
{
public string Name { get; set; }
public bool IsActive { get; set; }
}
and my actions:
[HttpPost]
public JsonResult CreateType(ProductTypeCreateModel productType)
{
...
}
Thank.