Hello Team,
My ajax POST request fails to post data to the controller, I put a debugger into the javascript function and it loads all the data correctely but is not able to post them to the controller and to the databse.
$('#btnAdd').on('click', function () {
//jQuery Validations var isValidation = true; if (purchaseItems.length == 0) { isAllValid = false; alert('Please Add items to purchase!'); }
if ($('#SelectSupplier').val() == "0") { isAllValid = false; $('#SelectSupplier').siblings('span.error').css('visibility', 'visible'); } else { $('#SelectSupplier').siblings('span.error').css('visibility', 'hidden'); } if (isValidation) { purchaseItems.push({ ItemID: $('#selectItem').val(), ItemName: $('#selectItem option:selected').text(), Batch: $('#Batch').val(), Qty: $('#Qty').val(), CostPrice: $('#CP').val(), SellingPrice: $('#SP').val(), Expiry: $('#Expiry').val(), PurchaseID: $('#PurchaseID').val() + ' / ' + $('#SelectSupplier').val(), BonusIncluded: 0 });
$('#selectItem').val('0').focus(); $('#Batch, #Qty, #CP, #SP, #Expiry').val(''); } GeneratedItemsTable(); });
$('#btnSubmit').on('click', function () { var isAllValid = true;
if (purchaseItems.length == 0) { isAllValid = false; alert('Please Add items to purchase!'); }
if ($('#SelectSupplier').val() == "0") { isAllValid = false; $('#SelectSupplier').siblings('span.error').css('visibility', 'visible'); } else { $('#SelectSupplier').siblings('span.error').css('visibility', 'hidden'); }
if ($('#PurchaseID').val().trim() == '') { isAllValid = false; $('#PurchaseID').siblings('span.error').css('visibility', 'visible'); } else { $('#PurchaseID').siblings('span.error').css('visibility', 'hidden'); }
if ($('#InvocingDate').val() == '') { isAllValid = false; $('#InvocingDate').siblings('span.error').css('visibility', 'visible'); } else { $('#InvocingDate').siblings('span.error').css('visibility', 'hidden'); } if ($('#Amount').val() == '' || isNaN($('#Amount').val())) { isAllValid = false; $('#Amount').siblings('span.error').css('visibility', 'visible'); } else { $('#Amount').siblings('span.error').css('visibility', 'hidden'); }
if (isNaN($('#Discount').val())) { $('#Discount').siblings('span.error').css('visibility', 'visible'); } if (isNaN($('#Tax').val())) { $('#Tax').siblings('span.error').css('visibility', 'visible'); }
if (isAllValid) { var purchaseModel = { PurchaseID: $.trim($('#PurchaseID').val() + '--' + $('#SelectSupplier').val()), Date: $('#InvocingDate').val().trim(), SupplierID: $('#SelectSupplier').val(), Amount: $('#Amount').val(), Discount: $('#Discount').val(), Tax: $('#Tax').val(), GrandTotal: $('#GrandTotal').val(), IsPaid: $('#Payment').is(":checked") ? 1 : 0, Description: $('#Description').val(), PurchaseItems: purchaseItems } $(this).val('Please wait...'); //post data to server $.ajax({ url: '@Url.Action("SavePurchase", "PurchaseEntry")', type: "POST", dataType: "JSON", contentType: "application/json; chasert=utf-8", data: JSON.stringify({ purchaseModel: purchaseModel }), success: function (d) { //check is successfully save to database if (d.status == true) { //will send status from server side alert('Successfully done.'); location.reload(true); //clear form purchaseItems = []; $('#PurchaseID').val(''); $('#InvocingDate').val(''); $('#SelectSupplier').val('0');
} else { alert('Failed'); } $('#submit').val('Save'); }, error: function () { alert('Error. Please try again.'); $('#btnSubmit').val('Save'); } }); } });