Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 } 
})   
Posted

1 solution

When you serialize your form using 'serializeArray()', your boolean values might not be serialized correctly. I would manually serialize the form using the '.serialize()' method and then append the 'DataTable' array to it. I would use something like -

JavaScript
var formData = $("#PurchaseEnquiryDetails").serialize();

//Now convert your DataTable to JSON...
var aData = $('#dtPurchaseEnquiryLines').DataTable().rows().data().toArray();
var jsonData = JSON.stringify(aData);

//Then append the JSON data to your serialized form...
formData += '&oLines=' + encodeURIComponent(jsonData);

//Call Ajax...
$.ajax({
    type: "POST",
    url: "/PurchaseEnquiry/UpdatePEHeader",
    data: formData,
    success: function(response) {
        //Success msg here...
    },
    error: function(xhr, status, error) {
        //Error msg here...
    }
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900