Hello
I am trying to pass a form which contains a bool and DataTable which I have converted to an array as per the Datatable documentation.
var aData = $('#dtPurchaseEnquiryLines').DataTable().rows().data().toArray();
var formData = $("#PurchaseEnquiryDetails").serializeArray();
$.ajax({
type: "POST",
url: "/PurchaseEnquiry/UpdatePEHeader",
data: { oPE: jsonData, oLines: aData }
})
If I pass it using serializeArray the bool from the form is always passing back as false.
If i set the set as
var formData = $("#PurchaseEnquiryDetails").serialize();
the form is passing to the controller as null. If i remove the datatable array input from the ajax the from passes to the controller fine with the bool as true.
I have tried building an object for both inputs and posting this as stringify but then the datatable array is null on the controller albeit the form has passed correctly.
I do have a working solution which is
var aData = $('#dtPurchaseEnquiryLines').DataTable().rows().dat();
var formData = $("#PurchaseEnquiryDetails").serialize();
var jsonData = {};
$(formData).each(function (index, obj) {
if (obj.name === "ReviewRequired1" && !jsonData.hasOwnProperty(obj.name)) {
jsonData[obj.name] = obj.value; }
else if (!jsonData.hasOwnProperty(obj.name)) {
jsonData[obj.name] = obj.value; }
});
$.ajax({
type: "POST",
url: "/PurchaseEnquiry/UpdatePEHeader",
data: { oPE: jsonData, oLines: aData }
})
I just think that seems a bit of a fudge and not a good resolution. Could someone help ?
What I have tried:
var aData = $('#dtPurchaseEnquiryLines').DataTable().rows().dat();
var formData = $("#PurchaseEnquiryDetails").serialize();
var jsonData = {};
$(formData).each(function (index, obj) {
if (obj.name === "ReviewRequired1" && !jsonData.hasOwnProperty(obj.name)) {
jsonData[obj.name] = obj.value;
} else if (!jsonData.hasOwnProperty(obj.name)) {
jsonData[obj.name] = obj.value;
}
});
$.ajax({
type: "POST",
url: "/PurchaseEnquiry/UpdatePEHeader",
data: { oPE: jsonData, oLines: aData }
})